//autoStart.bat @echo off if "%1" == "h" goto begin mshta vbscript:createobject("wscript.shell").run("""%~nx0"" h",0)(window.close)&&exit :begin //上面是添加隐藏黑框框的 //下面是需要执行的命令 ::auth: linght ::date: 2018-01-09 ::system: windows ::auto start windows exe @set sleepTime=2 @set exeName=center_flextrbo.exe @set logPath=%~dp0 @REM exePath "C:\Program Files (x86)\Jiketong\simpleTrbo\center\" @set exePath=%logPath:centerAutoRestart=center% @set runLogPath="%exePath%runLog.log" :loop @set aTxtfilePath="%logPath%a.txt" @tasklist | findstr %exeName% > %aTxtfilePath% @for /f "tokens=3 delims=:" %%a in (\' find /c /v "" %aTxtfilePath% \') do @set/a result=%%a+0 @if %result% LSS 1 goto reStart @if %result% equ 1 goto normal @if %result% gtr 1 goto killExe @echo off :restart echo %exeName% is stop RunNum: %result%, auto start %exeName% rem start your program set d=%date:~0,10% set t=%time:~0,8% @set startTxtfilePath="%logPath%start.log" echo %d% %t%: start /b /d %exePath% %exeName% >> %startTxtfilePath% REM start /b /d "%exePath%" %exeName% | tee %runLogPath% ping 0.0.0.0 -n %sleepTime% > "%logPath%null" start /b /d "%exePath%" %exeName% > %runLogPath% @goto sleep :normal rem echo %exeName% is normal runing...... RunNum: %result% @goto sleep :killExe echo %exeName% is running, RunNum: %result%, too mush, kill and restart taskkill /f /im %exeName% /t echo auto restart %exeName% goto reStart @goto sleep @echo on :sleep @echo off rem sleep 10s ping 0.0.0.0 -n %sleepTime% > "%logPath%null" goto loop echo on :over
这个方法相当巴适,比用vbs调用bat来隐藏黑框框棒多了。
其实,vbs调用bat来隐藏黑框框也还是可以的,不过呢,我遇到的场景是:我用vbs(如:autoStart.bat)调用bat(如:autoStart.bat)来隐藏黑框框。
但是呢,我的bat(如:autoStart.bat)需要增加授予管理员权限运行(看此链接),以便PC机任何用户登录都可以正确的执行。但是呢,我把授权的几行命令加在当前bat最上头的时候啊,bat内部调用程序就变成找不到文件(如上面加粗并标红的start命令)。
这个就恼火了,因为同样的授予管理员权限方式,别的bat(如:start_mysql.bat)就可以好好的运行。
这下啷个办呢,就把授权的命令,单独的保存为一个bat(如:grantAdmin.bat),然后在grantAdmin.bat里调用我的bat(autoStart.bat)。还是用vbs调用bat来隐藏黑框框(如:autoStart.vbs调用grantAdmin.bat)。问题来了,grantAdmin.bat的黑框框确是被隐藏了,autoStart.bat的黑框并没有隐藏,说以啊,在网上找啊找,寻寻觅觅两天时间,终于搞定,那就是用当前这边文章的方法来隐藏黑框框。棒的很。
//grantAdmin.bat @ echo off %1 %2 ver|find "5.">nul&&goto :Admin mshta vbscript:createobject("shell.application").shellexecute("%~s0","goto :Admin","","runas",1)(window.close)&goto :eof :Admin start autoStart.bat
//autoStart.vbs public const vbQuote="""" temp="C:\A_WORK\Release_D1.1.11\SimpleTrbo_Release1012\centerAutoRestart\grantAdmin.bat" batPath=vbQuote & temp & vbQuote Set wshell=CreateObject("WScript.Shell") wshell.Run batPath,0,false Set wshell=Nothing
谢谢你阅读
[小花儿送给你]