Batch Script to start stopped service.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
rajiv.rnv
Posts: 4
Joined: 10 May 2013 00:09

Batch Script to start stopped service.

#1 Post by rajiv.rnv » 10 May 2013 00:25

Hi All,

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

:PROCESSCHECK
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
)

)

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Batch Script to start stopped service.

#2 Post by Endoro » 10 May 2013 02:46

inside an "if" or "for" code block you have to use !variables! with delayed expansion instead of %variables%.

rajiv.rnv
Posts: 4
Joined: 10 May 2013 00:09

Re: Batch Script to start stopped service.

#3 Post by rajiv.rnv » 10 May 2013 12:13

Thanks.. it worked in below way
set process=%process%%%i
set serviceName=%serviceName%%%j
but now I am not sure why it is not able to call STARTPROCESS and command is not going to the function call. it gets stuck when it sees goto END.

Please help

:PROCESSCHECK
FOR /F "tokens=1,2 usebackq delims=," %%i in (P:\TCI-WORK\MyWork\process.txt) DO (

set process=%process%%%i
set serviceName=%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 inside 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 with APS-AS TCI >> %LOG_FILE%
pause
exit 1
)

)

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Batch Script to start stopped service.

#4 Post by Endoro » 10 May 2013 14:05

try this:

Code: Select all

@echo off &setlocal
:PROCESSCHECK
FOR /F "tokens=1,2 usebackq delims=," %%i in ("P:\TCI-WORK\MyWork\process.txt") DO (
   set "process=%%i"
   set "serviceName=%%j"
   echo %%i is process
   echo %%j is serviceName
   for /f "delims=" %%Z in ('tasklist /nh /fi "imagename eq %%i"') DO (
      IF "%%Z"=="INFO:" (
         echo %datetime%  Process %%i is not running >> %LOG_FILE%
         call:STARTPROCESS
      ) ELSE (
         echo %datetime%  Process %%i is running >> %LOG_FILE%
      )
   )
)
goto:eof

:STARTPROCESS
echo inside startprocess
echo STart "" %process%
echo Net start %service%
echo %datetime%  STARTING THE PROCESS %process% >> %LOG_FILE%
for /f "delims=" %%P in ('tasklist /nh /fi "imagename eq %process%"') DO (
   IF "%%P"=="INFO:" (
      echo %datetime%  ALARM ==^> Process %process% has not started please check with APS-AS TCI >> %LOG_FILE%
      pause
      exit 1
   )
)
goto:eof


Please note: you use %service% and %serviceName% (imo).

rajiv.rnv
Posts: 4
Joined: 10 May 2013 00:09

Re: Batch Script to start stopped service.

#5 Post by rajiv.rnv » 10 May 2013 14:17

Thanks I will try and let u know.

rajiv.rnv
Posts: 4
Joined: 10 May 2013 00:09

Re: Batch Script to start stopped service.

#6 Post by rajiv.rnv » 11 May 2013 04:20

I put only two lines in process.txt:
calc.exe,My Calculator
notepad.exe,My Notepad

I copy pasted your code as it is with echo on. below is the out put, seems not working.. :(



C

Code: Select all

:\WINDOWS>P:\TCI-WORK\MyWork\test.bat

C:\WINDOWS>set LOGFILE="P:\TCI-WORK\MyWork\log.txt"

C:\WINDOWS>FOR /F "tokens=1,2 usebackq delims=," %i in ("P:\TCI-WORK\MyWork\proc
ess.txt") DO (
set "process=%i"
 set "service=%j"
 echo notepad.exe is process
 echo My Notepad is serviceName
 for /F "delims=" %Z in ('tasklist /nh /fi "imagename eq %i"') DO (IF "%Z" == "I
NFO:" (
echo Sa- 0-/11/_180618  Process %i is not running  1>>"P:\TCI-WORK\Process_check
.txt"
 call:STARTPROCESS
)  ELSE (echo Sa- 0-/11/_180618  Process %i is running  1>>"P:\TCI-WORK\Process_
check.txt" ) )
)

C:\WINDOWS>(
set "process=calc.exe"
 set "service=My Calculator"
 echo notepad.exe is process
 echo My Notepad is serviceName
 for /F "delims=" %Z in ('tasklist /nh /fi "imagename eq calc.exe"') DO (IF "%Z"
 == "INFO:" (
echo Sa- 0-/11/_180618  Process calc.exe is not running  1>>"P:\TCI-WORK\Process
_check.txt"
 call:STARTPROCESS
)  ELSE (echo Sa- 0-/11/_180618  Process calc.exe is running  1>>"P:\TCI-WORK\Pr
ocess_check.txt" ) )
)
notepad.exe is process
My Notepad is serviceName

C:\WINDOWS>(IF "INFO: No tasks are running which match the specified criteria."
== "INFO:" (
echo Sa- 0-/11/_180618  Process calc.exe is not running  1>>"P:\TCI-WORK\Process
_check.txt"
 call:STARTPROCESS
)  ELSE (echo Sa- 0-/11/_180618  Process calc.exe is running  1>>"P:\TCI-WORK\Pr
ocess_check.txt" ) )

C:\WINDOWS>(
set "process=notepad.exe"
 set "service=My Notepad"
 echo notepad.exe is process
 echo My Notepad is serviceName
 for /F "delims=" %Z in ('tasklist /nh /fi "imagename eq notepad.exe"') DO (IF "
%Z" == "INFO:" (
echo Sa- 0-/11/_180618  Process notepad.exe is not running  1>>"P:\TCI-WORK\Proc
ess_check.txt"
 call:STARTPROCESS
)  ELSE (echo Sa- 0-/11/_180618  Process notepad.exe is running  1>>"P:\TCI-WORK
\Process_check.txt" ) )
)
notepad.exe is process
My Notepad is serviceName

C:\WINDOWS>(IF "notepad.exe                  10740 ICA-tcp#37                 5
    20,916 K" == "INFO:" (
echo Sa- 0-/11/_180618  Process notepad.exe is not running  1>>"P:\TCI-WORK\Proc
ess_check.txt"
 call:STARTPROCESS
)  ELSE (echo Sa- 0-/11/_180618  Process notepad.exe is running  1>>"P:\TCI-WORK
\Process_check.txt" ) )

C:\WINDOWS>(IF "notepad.exe                  10796 ICA-tcp#37                 5
    22,356 K" == "INFO:" (
echo Sa- 0-/11/_180618  Process notepad.exe is not running  1>>"P:\TCI-WORK\Proc
ess_check.txt"
 call:STARTPROCESS
)  ELSE (echo Sa- 0-/11/_180618  Process notepad.exe is running  1>>"P:\TCI-WORK
\Process_check.txt" ) )

C:\WINDOWS>(IF "notepad.exe                  13844 ICA-tcp#48                 1
       552 K" == "INFO:" (
echo Sa- 0-/11/_180618  Process notepad.exe is not running  1>>"P:\TCI-WORK\Proc
ess_check.txt"
 call:STARTPROCESS
)  ELSE (echo Sa- 0-/11/_180618  Process notepad.exe is running  1>>"P:\TCI-WORK
\Process_check.txt" ) )

C:\WINDOWS>goto:eof

Post Reply