I have a requirement where I have to write a dos script which checks stoped services and starts it. Though I am completely new in DOS script , I tried but my code is not working. here is the code. My logic is as below
1. process.txt is a file having the process and service name delimeted by comma.
2. we will iterate and read the process.txt split the comma separated store the process in one variable and service name in another variable.
3. Check the procvess is running or not, if not then call net start service name.
But after splitting the process and service name I am unable to store in a variable
calc.exe is process
My Calculator is service
ERROR: The search filter cannot be recognized.
Please help.
goto PROCESSCHECK
:END
echo ======================================================== >> %LOG_FILE%
echo ======================================================== >> %LOG_FILE%
pause
exit 0

FOR /F "tokens=1,2 usebackq delims=," %%i in (P:\MyWORK\process.txt) DO (
set process="%%i"
set serviceName="%%j"
echo %%i is process
echo %%j is service
for /f "usebackq delims=" %%Z in (`tasklist /nh /fi "imagename eq %process%"`) DO (
IF %%Z == INFO: (
echo %datetime% Process %%i is not running >> %LOG_FILE%
call:STARTPROCESS
) ELSE (
echo %datetime% Process %process% is running >> %LOG_FILE%
)
)
)
goto END
:STARTPROCESS
echo STart "" %process%
echo Net start %service%
echo %datetime% STARTING THE PROCESS %process% >> %LOG_FILE%
for /f "usebackq" %%P in (`tasklist /nh /fi "imagename eq %process%"`) DO (
IF %%P == INFO: (
echo %datetime% ALARM ==> Process %process% has not started please check >> %LOG_FILE%
pause
exit 1
)
)