windows下bat批處理實現守護進程(有日誌)

來源:互聯網
上載者:User

標籤:

開發部的一個核心程式總是會自己宕機,然後需要手工去起,而這個服務的安全層級又很高,只有我可以操作,搞得我晚上老沒法睡,昨晚實在受不了了,想起以前在hp-ux下寫的shell守護進程,這回搞個windows下的bat版精靈吧,當時晚上思路已經很遲鈍了,就叫了個兄弟讓他寫了,上去後運行效果不錯,至少昨晚我安心睡了7小時。早上來把程式改完善一些,增加了記錄等功能。實現:

檢查是否有notepad,要用的話就算成自己的進程名,如果進程宕了就過會自動重啟(會在目前的目錄下產生一個start.bat)

@echo off

set _task=notepad.exe
set _svr=c:/windows/notepad.exe
set _des=start.bat

:checkstart
for /f "tokens=5" %%n in (‘qprocess.exe ^| find "%_task%" ‘) do (
if %%n==%_task% (goto checkag) else goto startsvr
)



:startsvr
echo %time% 
echo ********程式開始啟動********
echo 程式重新啟動於 %time% ,請檢查系統日誌 >> restart_service.txt
echo start %_svr% > %_des%
echo exit >> %_des%
start %_des%
set/p=.<nul
for /L %%i in (1 1 10) do set /p a=.<nul&ping.exe /n 2 127.0.0.1>nul
echo .
echo Wscript.Sleep WScript.Arguments(0) >%tmp%/delay.vbs 
cscript //b //nologo %tmp%/delay.vbs 10000 
del %_des% /Q
echo ********程式啟動完成********
goto checkstart


:checkag
echo %time% 程式運行正常,10秒後繼續檢查.. 
echo Wscript.Sleep WScript.Arguments(0) >%tmp%/delay.vbs 
cscript //b //nologo %tmp%/delay.vbs 10000 
goto checkstart

 

http://blog.csdn.net/jiangxinyu/article/details/5217342

windows下bat批處理實現守護進程(有日誌)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.