Auto restarting a program with batch - batch-file

My current program runs its tasks, then closes itself, the batch file then restarts it after 60 seconds. The problem is that sometimes the program is unable to close itself, and everything is stuck.
I need to modify the script, so it will autorestart after 5 minutes, if the program does not close itself.
:launch
START /wait program.exe
rem delay 60 seconds
ping 127.0.0.1 -n 60 > nul
GOTO :launch

You can do something like that :
For example you check for every 5 minutes if the calc.exe process is running or not.
#echo off
Mode con cols=55 lines=3
:CheckRunningProcess
Cls
echo(
Set "MyProcess=calc.exe"
Title Check for Running Process "%MyProcess%"
tasklist /NH /FI "imagename eq %MyProcess%" 2>nul |find /i "%MyProcess%" >nul
If not errorlevel 1 ( Color 0A & Echo "%MyProcess%" is running
) else (Color 0C & echo "%MyProcess%" is not running, so we start it right now & start "" "%MyProcess%")
TimeOut /T 300 /NoBreak >nul
Goto :CheckRunningProcess

Related

Keep running batch loop after starting .exe

My program.exe sometimes stops. I have made batch script that checks if program is running and start it if not in loop.
The problem is loop is exiting after program.exe is started and runned.
I need to keep running the loop to keep checking every 5 mins if program still running or needs to be started again.
set loopcount=10000
:loop
tasklist /FI "IMAGENAME eq program.exe" /FO CSV > search.log
FINDSTR program.exe search.log > found.log
FOR /F %%A IN (found.log) DO IF %%~zA EQU 0 GOTO end
echo Starting..
start /b C:\_Program\program.exe
:end
del search.log
del found.log
echo Waiting..
timeout /t 300 /nobreak
if %loopcount%==0 goto exitloop
goto loop
:exitloop
pause
Batch File
If you have to use a batch file, I'd suggest:
Removing the loop
Running it every 5 minutes using a scheduled task
A scheduled task is more robust than an infinite loop in case the process crashes.
Run this once to schedule a task that repeats every 5 minutes:
schtasks /CREATE /SC DAILY /MO 1 /TN 'Name To Give the Scheduled Task' /TR 'C:\path\to\your\script.bat' /ST 0:00 /RI 5 /DU 24:00
PowerShell
If you can use PowerShell, the equivalent is a bit simpler:
if ($null -eq (ps program -ErrorAction SilentlyContinue)) {
saps C:\_Program\program.exe
}
Run this once to schedule a task that repeats every 5 minutes:
schtasks /CREATE /SC DAILY /MO 1 /TN 'Name To Give the Scheduled Task' /TR 'powershell -EB C:\path\to\your\script.ps1' /ST 0:00 /RI 5 /DU 24:00
It might be as simple as:
#echo off
:repeat
tasklist | findstr /i "program.exe">nul
if not %errorlevel% equ 0 start /b "C:\_Program\program.exe"
timeout /t 10 /nobreak>nul && goto :repeat
This does tasklist and we use findstr to determine errorlevel if not 0 start program, timeout for 10 seconds and repeat, no external files needed.
Here is an example to test.
To achieve continuously check (in loop) the existence of process "WinRAR.exe" (as an example of application to check); so you can change of course the path and the process name to check.
#echo off
Set "MyApplication=%Programfiles%\WinRAR\WinRAR.exe"
Set "MyProcess=WinRAR.exe"
Color 9B
Title Check Running process "%MyProcess%"
mode con cols=75 lines=2
:Loop
cls
tasklist /nh /fi "imagename eq %MyProcess%" 2>nul |find /i "%MyProcess%" >nul
If not errorlevel 1 (Echo "%MyProcess%" is running) else (start "" "%MyApplication%")
ping -n 60 127.0.0.1 >nul
goto Loop

Running a Batch File that Checks for a Process if not running Run a batch file Windows 10

Windows 10 I can't seem to get the batch file to activate the second batch file if the process is not running. First part works fine, calling the second batch file is broken. I tried to call, I tried %~dp0, like this: %~dp0MyProgram.bat
#echo off
:start
cls
set proc=zm.exe
set runme=c:\ZM\zcash.bat
set time=30
tasklist /FI "IMAGENAME eq %proc%" 2>NUL | find /I /N "%proc%">NUL
if "%ERRORLEVEL%"=="0" (
echo Everything is OK
timeout /t %time%
goto :start ) || (
echo ERROR! restarting service
taskkill /IM "%proc%" /F
else (start "" "%runme%")
)
timeout /t %time%
goto :start
Despite saying you'd tried it, you should use Call because you're wanting to return to the same script upon it's completion.
Perhaps something like this would work for you?
#Echo off
Set "proc=zm.exe"
Set "runme=C:\ZM\zcash.bat"
Set "secs=30"
:Loop
ClS
TaskList | FindStr /I "\<%proc%\>" >Nul && (Echo Everything is OK) || (
Echo ERROR! starting service
Call "%runme%")
Timeout %secs% /NoBreak >Nul
GoTo Loop
Note: I changed the variable name to %secs% because %TIME% is already a defined system variable.

How to Kill a Command after certain period of time in Command Prompt even if it is running state

I am running a command in command prompt and I want to kill it after 15 minutes. I don't care if it is completed or not. I don't want to have any human interaction to kill the running command such as someone has to press CTRL+C to kill it.
Is there a way to do it.
Please note I don;t want to use any third party tools or scripts.
start forfiles /s
timeout /t 5
Taskkill /im forfiles.exe /f
is one way.
Did you mean something like that ?
#echo off
Tracert www.google.com
timeout /t 900 /nobreak >NUL
Taskkill /im cmd.exe /f
Edit : based on the blueray's comment :
how can I put this command in a single line to use in CMD?
Tracert www.google.com & timeout /t 900 /nobreak >NUL & Taskkill /im cmd.exe /f
May be this could help
start your-command-here
ping 127.0.0.1 -n 120 > NUL
taskkill /im cmd.exe /f
Explanation:
start: this command starts executing your command
ping: it pings your local machine(ip : 127.0.0.1) for 120 sec and >NUL redirects the output to nowhere else the output of ping command will be displayed on the cmd screen
taskkill: it is used to kill any task
/im: image name of the process to be terminated. if the command is running on cmd then cmd.exe or any program that you need to kill.
Hope it helps.
Try this, works well for me:
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
#echo off
set _time=0
set _timeout=30
start myprocess.exe
:waitforprocess
set /A _time=_time+1
IF !_time! GTR !_timeout! goto TimedOut
rem TaskList will return the task list of the image as specific
rem or it will return INFO: No tasks are running.... so look for
rem the INFO statement using FindStr. FindStr will return a errorlevel
rem of 0 if it found the string and a 1 if it did not, so use that
rem to work out next steps.
rem -------------------------------------------------------------------
tasklist /NH /FI "IMAGENAME EQ myprocess.exe" | findstr INFO
rem ERRORLEVEL 1 = Did not find the string, so try again.
IF ERRORLEVEL 1 (
timeout /T 1 /NOBREAK > nul
goto :waitforprocess
)
GOTO DONE
:TimedOut
ECHO We timedout so will kill the process
taskkill /FI "IMAGENAME EQ myprocess.exe" /T /F
:Done
echo finished

Jenkins - Batch wait till process is up and kill if it hangs

I’m using Jenkins on Windows and want to run csscript that launch an application and execute automatic tests, now I want to create a time out so in case the application is running more than about 30 minutes it will be killed,
I have this batch script: first I create a FOR loop that wait till the application is up and then another FOR loop that check if the application is up more than 25 minutes.
The problem is I’m getting this error in the first loop
Process leaked file descriptors. See http://wiki.jenkins-ci.org/display/JENKINS/Spawning+processes+from+build for more information
I read the info on wiki but didn’t really understand how to solve my issue
Please help
start cscript //nologo D:\tets.vbs
FOR /L %%A IN (1,1,20) DO (
echo Round number %%A
REM find the running executable
tasklist | find /I /C "App.exe" > nul
echo ERRORLEVEL is !ERRORLEVEL!
IF !ERRORLEVEL! EQU 0 EXIT
rem wait 3 seconds
ping 1.1.1.1 -n 1 -w 3000 > nul
)
FOR /L %%U IN (1,1,50) DO (
echo Round number %%U
REM find the running executable
tasklist | find /I /C "App.exe" > nul
echo ERRORLEVEL is !ERRORLEVEL!
if !ERRORLEVEL! EQU 1 EXIT
rem wait 30 seconds
ping 1.1.1.1 -n 1 -w 30000 > nul
)
echo TASKILL
taskkill /f /im App.exe
If you want to timeout a build, use the Build-timeout plugin
Edit:
The suggestion for the article you linked is to use the at command. Instead of:
start cscript //nologo D:\tets.vbs
use
at %time% start cscript //nologo D:\tets.vbs
You will have to calculate the next %time% (in minutes), and obviously the job will have to wait for that minute to start, so at best a 1 second delay, at worse a 59 second delay.

How to start an .exe file using input/output file and set a time limit in batch?

In windows command prompt, when I want to run a program using input/output file, I always use batch command like the following: test.exe < input.in > output.out.
(test.exe is the name of program, input.it is the name of input file and output.out is the name of output file)
But if I use this command, I cannot set a time limit for that program (i.e. I cannot force the program to quit after an amount of time).
So what command I should use in order to do that? Thank you for helping me.
You can use taskkill command:
start test.exe
ping 127.0.0.1 -n 5
taskkill /im test.exe /f
Here it's killed after 5 seconds. You can specify any duration in seconds.
start "" /b cmd /c "test.exe <input.in >output.out"
timeout /t 10
tasklist | find "test.exe" >nul && taskkill /f /im test.exe
Start the program inside a cmd instance not attached to the current console, wait for 10 seconds and if program still in task list, kill it
EDIT - Updated to handle the case pointed in comments
#echo off
setlocal enableextensions
start "" /b cmd /c "test.exe <input.in >output.out"
call :timeoutProcess "test.exe" 10
if errorlevel 1 (
echo Program has been killed
) else (
echo Program has ended in time
)
exit /b
:timeoutProcess process timeout
for /l %%t in (1 1 %~2) do (
timeout.exe /t 1 >nul
tasklist | find "%~1" >nul || exit /b 0
)
taskkill /f /im "%~1" >nul
exit /b 1

Resources