[CMD]内存监测脚本 📅 2026/6/30 23:48:55 一个快捷方式打开脚本当某个进程的内存满了3G时运行脚本Windows 系统实现方法使用 PowerShell 脚本和计划任务首先编写 PowerShell 脚本用于检查进程内存并在满足条件时运行目标脚本。假设要检查的进程名为targetProcess.exe目标脚本为yourScript.ps1。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#!/bin/bash processNametargetProcess 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 时执行另一个脚本的功能。注意根据实际情况调整路径和进程名称等参数。