S.l.e!ep.¢%

像打了激速一样,以四倍的速度运转,开心的工作
简单、开放、平等的公司文化;尊重个性、自由与个人价值;
posts - 1098, comments - 335, trackbacks - 0, articles - 1
  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

P处理的双进程守护

Posted on 2009-08-12 21:16 S.l.e!ep.¢% 阅读(562) 评论(1)  编辑 收藏 引用 所属分类: DOS
@echo off & SetLocal EnableDelayedExpansion

set "self=%~0"
if /i "%~1"=="ExitCode" goto :EXIT
if /i "%~1"=="" echo 现在是测试运行
if /i "%~1"=="WIN_Exit" echo 貌似我又复活了
echo start "" "%~0" WIN_Exit>ErrExit.cmd
call :查找进程

call :FINDEXISTPID

call :守护进程

start Check.vbs
:REINPUT
        echo -----------------------------------------
        echo  输入:ExitCode 完全退出程序
        echo -----------------------------------------
        set /p "Input=请输入入口代码:"
        if /i "%Input%"=="ExitCode" goto :EXITPROCESS_OK
goto :REINPUT

:查找进程
echo for each ps in getobject _>ps.vbs
echo ^("winmgmts:\\.\root\cimv2:win32_process"^).instances_ >>ps.vbs
echo if ps.name ="cmd.exe" Then wscript.echo ps.handle^&vbtab^&ps.name>>ps.vbs
echo next>>ps.vbs
goto :EOF

:FINDEXISTPID
        if exist ExistPid.txt del ExistPid.txt
        for /f "tokens=1,* " %%m in ('cscript //nologo ps.vbs') do echo %%m>>ExistPid.txt
        for /f "tokens=1,* " %%m in ('cscript //nologo ps.vbs') do (
                type ExistPid.txt | find /i "%%m">nul && set "Pid=%%m"
                )
goto :EOF

:守护进程
echo set Cleaner=createobject^("wscript.shell"^)>Check.vbs
echo do>>Check.vbs
echo WScript.Sleep 100>>Check.vbs
echo if not isProcessExists^("%Pid%"^) then>>Check.vbs
echo Cleaner.run "ErrExit.cmd">>Check.vbs
echo Exit do>>Check.vbs
echo End if>>Check.vbs
echo loop>>Check.vbs
echo Function isProcessExists^(proName^)>>Check.vbs
echo Set objWMIService = GetObject^("winmgmts:" ^& "{impersonationlevel=impersonate}^!\\.\root\cimv2"^)>>Check.vbs
echo Set colProcessList = objWMIService.ExecQuery^("Select * from Win32_Process Where Handle='" ^& proName ^& "'"^)>>Check.vbs
echo isProcessExists = ^(colProcessList.Count ^<^> 0^)>>Check.vbs
echo End Function>>Check.vbs
goto :EOF
:EXITPROCESS_OK
echo for each ps in getobject _>ps.vbs
echo ^("winmgmts:\\.\root\cimv2:win32_process"^).instances_ >>ps.vbs
echo if ps.name ="WScript.exe" Then wscript.echo ps.handle^&vbtab^&ps.name>>ps.vbs
echo next>>ps.vbs

for /f "tokens=1,* " %%i in ('cscript //nologo ps.vbs') do ntsd -c q -p %%i
echo del ps.vbs>>deltemp.cmd
echo del check.vbs>>deltemp.cmd
echo del ErrExit.cmd>>deltemp.cmd
echo del ExistPid.txt>>deltemp.cmd
echo del run.vbs>>deltemp.cmd
echo del deltemp.cmd>>deltemp.cmd
echo set Cleaner=createobject^("wscript.shell"^)>>run.vbs
echo Cleaner.run "cmd /c deltemp.cmd",^0>>run.vbs
start run.vbs

:EXIT
exit

Feedback

# re: P处理的双进程守护  回复  更多评论   

2013-04-28 19:29 by 幻想
这个感觉变麻烦了,用tasklist 然管道给 findstr 查找进程名,然后再放入for /f 中查看是否有数据就可以了.不需要间接地调用vbs,如果用vbs的话就不需要用bat这个撇脚的东西了奴.

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理