開發部的一個核心程序老是會本身宕機,而後須要手工去起,而這個服務的安全級別又很高,只有我能夠操做,搞得我晚上老無法睡,昨晚實在受不了了,想起之前在hp-ux下寫的shell守護進程,這回搞個windows下的bat版守護程序吧,當時晚上思路已經很遲鈍了,就叫了個兄弟讓他寫了,上去後運行效果不錯,至少昨晚我安心睡了7小時。html
檢查是否有notepad,要用的話就算成本身的進程名,若是進程宕了就過會自動重啓(會在當前目錄下生成一個start.bat)java
@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
sql
出處:https://blog.csdn.net/jiangxinyu/article/details/5217342shell
========================================================================vim
@echo off
::檢測時間間隔,單位:秒
set _interval=5
::須要守護的進程名稱
set _processName=WinRAR
::須要守護的進程啓動命令
set _processCmd=C:\ideavim-0.32.zip
::須要守護的進程預估啓動完畢所需時間,單位:秒
set _processTimeout=10
::須要守護的進程所監聽的端口
set _port=8080
::進程用戶名,通常是Administrator
set _username=Administrator
:LOOP
set /a isAlive=false
::經過進程名稱檢測
::tasklist /FI "username eq %_username%" | find /C "%_processName%" > temp.txt
::set /p num= < temp.txt
::del /F temp.txt
::經過進程的端口是否正在被監聽檢測
netstat -an | find /C "0.0.0.0:%_port%" > temp.txt
set /p num= < temp.txt
del /F temp.txt
if "%num%" == "0" (
start %_processCmd% | echo 啓動 %_processName% 於 %time%
choice /D y /t %_processTimeout% > nul
)
if "%num%" NEQ "0" echo 已啓動
::ping -n %_interval% 127.1>nul
choice /D y /t %_interval% >nul
goto LOOP
該腳本運行後會出現一個dos窗口,若以爲礙眼能夠用記事本將下面保存爲擴展名爲.vbs的文件。其中guard.bat是上面批處理保存的文件名。兩個文件放在同一目錄下雙擊vbs文件後,守護進程就在後臺運行了,單這樣得經過資源管理器結束cmd進程才能中止守護進程。windows
set ws=WScript.CreateObject("WScript.Shell")
ws.Run "guard.bat",0
出處:https://blog.csdn.net/qin9r3y/article/details/22805095安全
==================================================================ide
根據上面的內容本身修改一個放到項目上使用:測試
1)能夠根據參數,執行指定的程序優化
2)能夠保存詳細的執行日誌
版本一:
@echo off title processWatch cls set d=%date:~0,10% set d=%d: =0% echo %d% set logFile=.\processWatch_%d:-=%.txt echo 如今時間:%date%%time% echo ********程序開始啓動******** >> %logFile% set runAppPath=%1 set runAppPath="C:\Users\Administrator\Desktop\新建文件夾\Cares.AutoUpdate.exe" set runAppFolder='' if '%runAppPath%'=='' (goto end) set _processName='' set _processNameExt='' ::檢測時間間隔,單位:秒 set _interval=10 for %%a in (%runAppPath%) do ( set _processName=%%~na set _processNameExt=%%~nxa set runAppFolder=%%~dpa ) goto checkstart rem 如下是測試代碼 echo %_processName% echo %_processNameExt% echo %runAppFolder% echo %runAppPath% echo ==================================================== goto end :checkstart set d=%date:~0,10% set d=%d: =0% set logFile=.\processWatch_%d:-=%.txt for /f "tokens=1" %%n in ('tasklist.exe ^| find /I "%_processNameExt%" ') do ( if '%%n'=='%_processNameExt%' (echo 檢查進程【%_processNameExt%】正在運行 >> %logFile% && goto checkend) ) echo 檢查進程【%_processNameExt%】未在運行 >> %logFile% :startApp echo, && echo ********開始啓動程序******** echo ********開始啓動程序******** >> %logFile% echo [%date:~0,10%%time:~0,8%] 未檢測到【%_processNameExt%】的進程,將從新啓動程序 >> %logFile% echo [%date:~0,10%%time:~0,8%] 從新啓動程序:%runAppPath% echo [%date:~0,10%%time:~0,8%] 從新啓動程序:%runAppPath% >> %logFile% echo 開始執行 start "" %runAppPath% >> %logFile% pushd %runAppFolder% start "" %runAppPath% popd echo ********啓動程序完成******** && echo, echo ********啓動程序完成******** >> %logFile% echo, >> %logFile% :checkend echo [%date:~0,10%%time:~0,8%] 程序【%_processNameExt%】運行正常,%_interval%秒後繼續檢查...... choice /t %_interval% /d y /n >nul goto checkstart :end echo end.
後續將繼續優化