vbs系统后台持续检测一个进程是否存在

回答百度知道问题:
http://zhidao.baidu.com/question/713784585787480605

问题:
vbs,系统后台持续检测一个进程是否存在,如:qq.exe。如果存在,倒计时X秒将其关闭,如果不存在,继续后台检测。检测时间间隔可设置。

01Const INTERVA = 5  '检测间隔
02dim ps,ps_str
03Set objShell = createobject("wscript.shell")
04  
05REM 解决CMD黑色闪运行
07host = WScript.FullName
08If LCase(Right(host, 11)) = "wscript.exe" Then
09    objShell.run "cscript """ & WScript.ScriptFullName & chr(34), 0
10    WScript.Quit
11End If
12REM -----------------------------------------------------------------------
13  
14do while true
15    Set ps = objShell.Exec("tasklist")
16    ps_str = ps.Stdout.ReadAll
17    'msgbox ps_str
18    if find_yn(ps_str) then
19        objShell.Popup "5秒后将关闭 QQ !", 5, "Close QQ"
20        Set ps = objShell.Exec("taskkill /F /IM QQ.exe /T"'测试程序
21    end if
22    wscript.sleep 1000 * INTERVA  '检测间隔
23loop
24set ps = nothing
25set objShell = nothing
26  
27  
28Function find_yn(str_s)   '查找进程是否存在,不存在返回0
29    Dim re,ms,m
30    find_yn = 0
31    set re = New RegExp
32    re.Global = True
33    re.MultiLine = True
34    're.Pattern = "^(QQa|QQb|QQ).exe"     '可以检测查看多个进程
35    re.Pattern = "^QQ.exe"  '测试查找进程
36    set ms = re.Execute(str_s)
37    For Each m In ms
38        'msgbox m
39        find_yn = find_yn + 1
40    Next
41End Function

 

发表回复