Last active
August 29, 2015 14:02
-
-
Save prashanthrajagopal/9171b93f5c17e3d74835 to your computer and use it in GitHub Desktop.
Batch Script to restart a process if not already running. It runs every 5 seconds
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
:loop | |
REM Check if the process is already running | |
tasklist /fi "imagename eq %1" | find /i "%1" > nil | |
if errorlevel 1 ( | |
REM If not start the process | |
goto :main | |
) else ( | |
REM Else keep checking every 5 seconds whether it is alive | |
timeout /t 5 | |
goto :loop | |
) | |
:main | |
if [%2] == [] ( | |
start /wait %1 | |
) else ( | |
REM Use this if the process is to be run a s a different user | |
runas /profile /user:%2 "CMD /C %1" | |
) | |
goto :main | |
REM Usage example - start /B restart.bat notepad.exe |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment