使用vbs脚本来监控windows服务器上的应用程序(不存在就启动)

使用vbs脚本来监控windows服务器上的应用程序(不存在就启动)

这个vbs代码主要实现的功能就是运行该程序,就会在进程中出现一个wscript.exe 它会每隔10s扫面一次进程中是否存在notepad.exe这个程序,不存在就启动。这个启动程序可能跟进程名不一样,好比tomcat应用,启动的是startup.bat,后台进程名为java.exe,这样就需要调整代码proname= java.exe

关于如果关掉监控 可以在运行下执行taskkill /f /im wscript.exe 或者在任务管理器 进程中找到wscript.exe 结束进程就可以了

调整WshShell.Run( startup.bat )

核心代码

dim wmi,proc,procs,proname,flag,WshShellDoproname="notepadjb51.exe" '需要监测的服务进程的名称,自行替换这里的记事本进程名set wmi=getobject("winmgmts:{impersonationlevel=impersonate}!\.rootcimv2")set procs=wmi.execquery("select * from win32_process")flag=truefor each proc in procsif strcomp(proc.name,proname)=0 thenflag=falseexit forend ifnextset wmi=nothingif flag thenSet WshShell = Wscript.CreateObject("Wscript.Shell")WshShell.Run ("notepadjb51.exe")end ifwscript.sleep 10000 '检测间隔时

VBS写个小脚本 实时监测指定程序是否运行 对运行的软件进行操作 最后关闭运行软件

strComputer = "."Set objShell = CreateObject("Wscript.Shell") doSet wbemServices = Getobject("winmgmts:\\" strComputer)Set wbemObjectSet = wbemServices.InstancesOf("Win32_process")For Each wbemObject In wbemObjectSetif wbemObject.name="QQ.exe" then WScript.Sleep 1000 objShell.SendKeys "{F12}" WScript.Sleep 1000 objShell.SendKeys "{ENTER}" dim WSHshell set WSHshell = wscript.createobject("wscript.shell") WSHshell.run "taskkill /im wscript.exe /f ",0 ,true end ifNextloop

批处理检测进程是否存在;这个我用来检测文化进程是否存在,因为是绿色版的,很容易被关闭,所以需要检测。

tasklist /nh|find /i "ClientOfWatcher.exe"if ERRORLEVEL 1 (start C:\watcher\ClientOfWatcher.exe) else (exit)

VBS定时检测进程是否存在,如果不存在就启动进程。

option Explicit dim wmi,proc,procs,proname,flag,WshShell Do proname="ClientOfWatcher.exe" '需要监测的服务进程的名称,自行替换这里的记事本进程名 set wmi=getobject("winmgmts:{impersonationlevel=impersonate}!\\.\root\cimv2") set procs=wmi.execquery("select * from win32_process") flag=true for each proc in procs if strcomp(proc.name,proname)=0 then flag=false exit for end if next set wmi=nothing if flag then Set WshShell = Wscript.CreateObject("Wscript.Shell") WshShell.Run ("C:\Watcher\ClientOfWatcher.exe") end if wscript.sleep 50000 '检测间隔时间,这里是50秒 loop

关于vbs系统运行后,系统进程中产生大量wscript.exe

taskkill /IM wscript.exe /Ftaskkill /IM cscript.exe /Ftaskkill /IM consent.exe /Ftaskkill /IM ChsIME.exe /F

所有建议服务器上运行vbs后,如果出现大量wscript.exe进程,可以加上下面的命令

dim WSHshell set WSHshell = wscript.createobject("wscript.shell") WSHshell.run "taskkill /im wscript.exe /f ",0 ,true

使用vbs脚本来监控windows服务器上的应用程序(不存在就启动)