批量文件检查,重新检查并启动,如果没有运行说.exe无休止地

时间:2017-06-23 05:38:10

标签: batch-file command-prompt

我想使用批处理文件检查.exe是否正在运行,如果是,请设置ping 30秒并无休止地重新检查。如果.exe没有运行,则启动另一个.bat文件,然后再次检查一个不需要用户操作的常量循环。

我在这里检查了很多页面并尝试了很多东西,并且无法接缝以获得任何工作。如此生病提供更多细节。

我需要这个.bat文件一旦开始检查看看“7dtd.exe”是否正在运行,如果它正在运行然后等待,我已经看到整个ping null事件作为计时器大约30秒,然后重新检查如果“7dtd.exe”正在运行。如果它没有运行,那么我需要它来启动一个单独的“startdedi.bat”文件然后检查相同的“7dtd.exe”并继续无休止地检查。任何帮助都会很棒!

startdedicated.bat

@echo off
rem Starts a dedicated server
rem
rem -quit, -batchmode, -nographics: Unity commands
rem -configfile           : Allows server settings to be set up in an xml config file. Use no path if in same dir or full path.
rem -dedicated                    : Has to be the last option to start the dedicated server.

set LOGTIMESTAMP=


:: --------------------------------------------
:: REMOVE OLD LOGS (only keep latest 20)

for /f "tokens=* skip=19" %%F in ('dir 7DaysToDieServer_Data\output_log*.txt /o-d /tc /b') do del 7DaysToDieServer_Data\%%F



:: --------------------------------------------
:: BUILDING TIMESTAMP FOR LOGFILE

:: Check WMIC is available
WMIC.EXE Alias /? >NUL 2>&1 || GOTO s_start

:: Use WMIC to retrieve date and time
FOR /F "skip=1 tokens=1-6" %%G IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
    IF "%%~L"=="" goto s_done
    Set _yyyy=%%L
    Set _mm=00%%J
    Set _dd=00%%G
    Set _hour=00%%H
    Set _minute=00%%I
    Set _second=00%%K
)
:s_done

:: Pad digits with leading zeros
Set _mm=%_mm:~-2%
Set _dd=%_dd:~-2%
Set _hour=%_hour:~-2%
Set _minute=%_minute:~-2%
Set _second=%_second:~-2%

Set LOGTIMESTAMP=__%_yyyy%-%_mm%-%_dd%__%_hour%-%_minute%-%_second%

:s_start


:: --------------------------------------------
:: STARTING SERVER


echo|set /p="251570" > steam_appid.txt

start 7daystodieserver -logfile 7DaysToDieServer_Data\output_log%LOGTIMESTAMP%.txt -quit -batchmode -nographics -configfile=serverconfig.xml -dedicated


echo Starting server ...
timeout 15

cls

echo.
echo Server running in background, you can close this window.
echo You can check the task manager if the server process is really running.
echo.
echo.

pause

我尝试了这个命令,它接缝按照我需要的方式工作

@echo off
cls
:start
@echo Starting 7DTD Dedicated Test Server...

7daystodieserver -logfile 7DaysToDieServer_Data\output_log%LOGTIMESTAMP%.txt -quit -batchmode -nographics -

configfile=serverconfig.xml -dedicated

echo.
for /F "tokens=2" %%i in ('date /t') do set mydate=%%i
set mytime=%time%
echo Restarting Test Server: %mydate%:%mytime%
echo.
goto start

只要命令窗口在服务器关闭时保持运行,它就会重新启动它并让我知道服务器关闭的时间。再次感谢大家的帮助,这让我发疯了!!!

3 个答案:

答案 0 :(得分:1)

我相信Regejok的tasklist命令错误,条件命令也错了。

试试这个:

@ECHO OFF
:LOOP
TASKLIST /FI "IMAGENAME eq 7dtd.exe"|FIND "==">NUL&&(TIMEOUT 30 /NOBREAK>NUL
)||CALL startdedi.bat
GOTO LOOP

答案 1 :(得分:0)

是否必须ping等待?请改用timeout

此脚本将

  • 检查7dtd.exe是否正在运行
  • 如果未找到,请启动startdedi.bat并结束
  • 如果找到,请等待30秒并重新开始
:a
tasklist|findstr "7dtd.exe"||startdedi.bat
timeout 30
goto a

第二行列出所有任务,管道输出到findstr和'失败',即没有找到匹配,启动第二个.bat(我意识到有/fi参数,但是没有' t set errorlevel)。

如果您因任何原因坚持使用ping

ping -n 31 localhost

答案 2 :(得分:0)

经过一些研究并深入挖掘了startdedi.bat文件后,我尝试了这段代码,现在接缝工作正常。再次感谢所有的帮助和建议!这个社区很棒!

@echo off
cls
:start
@echo Starting 7DTD Dedicated Test Server...

@echo off
rem Starts a dedicated server
rem
rem -quit, -batchmode, -nographics: Unity commands
rem -configfile           : Allows server settings to be set up in an xml config file. Use no path if in same dir or full path.
rem -dedicated                    : Has to be the last option to start the dedicated server.

set LOGTIMESTAMP=


:: --------------------------------------------
:: REMOVE OLD LOGS (only keep latest 20)

for /f "tokens=* skip=19" %%F in ('dir 7DaysToDieServer_Data\output_log*.txt /o-d /tc /b') do del 7DaysToDieServer_Data\%%F



:: --------------------------------------------
:: BUILDING TIMESTAMP FOR LOGFILE

:: Check WMIC is available
WMIC.EXE Alias /? >NUL 2>&1 || GOTO s_start

:: Use WMIC to retrieve date and time
FOR /F "skip=1 tokens=1-6" %%G IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
    IF "%%~L"=="" goto s_done
    Set _yyyy=%%L
    Set _mm=00%%J
    Set _dd=00%%G
    Set _hour=00%%H
    Set _minute=00%%I
    Set _second=00%%K
)
:s_done

:: Pad digits with leading zeros
Set _mm=%_mm:~-2%
Set _dd=%_dd:~-2%
Set _hour=%_hour:~-2%
Set _minute=%_minute:~-2%
Set _second=%_second:~-2%

Set LOGTIMESTAMP=__%_yyyy%-%_mm%-%_dd%__%_hour%-%_minute%-%_second%

:s_start

7daystodieserver -logfile 7DaysToDieServer_Data\output_log%LOGTIMESTAMP%.txt -quit -batchmode -nographics -configfile=serverconfig.xml -dedicated

echo.
for /F "tokens=2" %%i in ('date /t') do set mydate=%%i
set mytime=%time%
echo Test Server was shutdown: %mydate%:%mytime%
echo.
goto start