一个快捷方式打开脚本,当某个进程的内存满了3G时运行脚本
Windows 系统实现方法
- 使用 PowerShell 脚本和计划任务:
- 首先,编写 PowerShell 脚本用于检查进程内存并在满足条件时运行目标脚本。假设要检查的进程名为
targetProcess.exe,目标脚本为yourScript.ps1。
- 首先,编写 PowerShell 脚本用于检查进程内存并在满足条件时运行目标脚本。假设要检查的进程名为
powershell
$processName = "targetProcess.exe"
$memoryThreshold = 3 * 1024 * 1024 * 1024 # 3GB
$targetScript = "C:\Path\To\yourScript.ps1"
$process = Get - Process - Name $processName - ErrorAction SilentlyContinue
if ($process) {
if ($process.WorkingSet64 - ge $memoryThreshold) {
& $targetScript
}
}
将上述脚本保存为 checkMemory.ps1。
然后,通过 Windows 计划任务来定时运行这个检查脚本。
- 打开 “任务计划程序”,在左侧窗格中选择 “任务计划程序库”。
- 在右侧窗格中点击 “创建任务”,在 “常规” 选项卡中为任务命名,例如 “Check Process Memory”。
- 在 “触发器” 选项卡中,设置任务的触发方式,比如选择 “按预定计划”,设置合适的时间间隔,如每 5 分钟运行一次。
- 在 “操作” 选项卡中,点击 “新建”,在 “程序或脚本” 框中输入
powershell.exe,在 “添加参数(可选)” 框中输入-ExecutionPolicy Bypass -File "C:\Path\To\checkMemory.ps1"。 - 最后,为了创建快捷方式,在桌面上右键点击,选择 “新建” -> “快捷方式”,在 “请键入对象的位置” 框中输入
powershell.exe -ExecutionPolicy Bypass -File "C:\Path\To\checkMemory.ps1",按照提示完成快捷方式创建。这样,双击快捷方式也能手动运行这个检查脚本。
Linux 系统实现方法
- 使用 Bash 脚本和 Cron 任务:
- 编写 Bash 脚本用于检查进程内存并在满足条件时运行目标脚本。假设要检查的进程名为
targetProcess,目标脚本为yourScript.sh。
- 编写 Bash 脚本用于检查进程内存并在满足条件时运行目标脚本。假设要检查的进程名为
bash
#!/bin/bash
processName="targetProcess"
memoryThreshold=$((3 * 1024 * 1024 * 1024))
targetScript="/path/to/yourScript.sh"
pid=$(pgrep $processName)
if [ -n "$pid" ]; then
memoryUsage=$(grep VmRSS /proc/$pid/status | awk '{print $2}')
memoryUsage=$((memoryUsage * 1024))
if [ $memoryUsage -ge $memoryThreshold ]; then
. $targetScript
fi
fi
将上述脚本保存为 checkMemory.sh,并赋予可执行权限 chmod +x checkMemory.sh。
- 设置 Cron 任务来定时运行这个检查脚本。编辑 Cron 表,输入
crontab -e,然后添加如下行来设置每 5 分钟运行一次脚本:
plaintext
*/5 * * * * /path/to/checkMemory.sh
- 要创建快捷方式,对于基于 GNOME 桌面环境的系统,可以在
~/.local/share/applications目录下创建一个.desktop 文件,例如checkMemory.desktop,内容如下:
ini
[Desktop Entry]
Name = Check Process Memory
Exec = /bin/bash /path/to/checkMemory.sh
Icon = /path/to/icon.png # 可选,指定图标路径
Terminal = true
Type = Application
Categories = Utility;
保存后,在应用程序菜单中就可以找到这个快捷方式,也可以将其拖到桌面等位置方便使用。
以上方法分别在 Windows 和 Linux 系统下实现了通过快捷方式运行脚本,当指定进程内存达到 3G 时执行另一个脚本的功能。注意根据实际情况调整路径和进程名称等参数。
5494

被折叠的 条评论
为什么被折叠?



