shell和bat 监控进程,自动关机

1.linux 下监控进程,进程结束后关机

新建文件 monit.sh

$chmod +x monit.sh

加入代码

#!/bin/sh

a=10;

while [ $a -gt 5 ]; do

if test $(pgrep -f $1|wc -l) -eq 2;then

echo "shutdown after 2 mins.."
shutdown -h 2

else
echo "$1 still running"
fi;
sleep 2;
done;

2.window下监控进程,结束关机

新建文本 monit.bat

贴代码:

:start
tasklist >tasklist.txt
find /i tasklist.txt "vmware-vmx.exe" 
if errorlevel 1 ((del /q tasklist.txt)&(goto end))
if errorlevel 0 (
echo "running"
ping 127.1 -n 20 >nul 2>nul
(goto start))
:end 
echo "shutdown after 60s"
ping 127.1 -n 20 >nul 2>nul
shutdown -s -t 50

这个也可以

@echo off
:1
tasklist | find "YodaoDict1exe" >>c:
otepad.luowei
if exist c:
otepad.luowei (
::taskkill /f /im YodaoDict.exe 
echo running
ping 127.1 -n 20 >nul 2>nul
) else (
echo 200s后关机
ping 127.1 -n 200 >nul 2>nul
shutdown -s -t 200
)
goto 1
原文地址:https://www.cnblogs.com/suyuan1573/p/4572654.html