我公司有台windows服务器,有个服务的一个进程总是自动断掉,我想写个脚本,用来监听进程,如果不在了,就调用管理员powershell,然后 执行重启服务


PowerShell交流中心分类: 进程和服务我公司有台windows服务器,有个服务的一个进程总是自动断掉,我想写个脚本,用来监听进程,如果不在了,就调用管理员powershell,然后 执行重启服务
0
Johnson asked 5年 ago

tasklist /FI “IMAGENAME eq nhttp.exe”| find /C “nhttp.exe” > temp.txt
set /p num= < temp.txt
del /F temp.txt
echo %num%
if “%num%” == “0” powershell /c “& {Start-Process powershell -Verb runas -ArgumentList /D/script/restart.psl}”

3 Answers
0
神经元短路 answered 5年 ago

while($true)
{
if((get-service spooler |select -ExpandProperty status) -eq “stopped”){Restart-Service spooler}
sleep 300
}
 

0
Johnson answered 5年 ago

进程断了,但是服务并不会当掉

0
神经元短路 answered 5年 ago

get-process可以找进程
if(!(get-process notepad -ea 4)){“no notepad is running”}