Solved

Check if a service or process is running.

Posted on 2007-03-25
11
975 Views
Last Modified: 2008-01-09
I need to write a batch file that checks to see if a particular service and process are running.  If either is not running I need to restart them.  I need to run this batch file everytime the computer is rebooted.
0
Comment
Question by:dsa_erie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18790159
How about:

sc stop "TermService"
sc start "TermService"

If you want to check first try this:

@echo off

setlocal

for /f "tokens=1,* delims=:" %%a in ('sc query "TermService" ^| findstr RUNNING') do sc stop "TermService"

sc start "TermService"

Good Luck,
Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 18791242
If this is truly a service rather than just an application process then have you considered using the options on the recovery tab of the service - look in properties of the service, recovery tab and you can choose to set it to restart service, generally on first and second failures only then maybe have it do something different on subsequent failures such as send a message to an admin.

If it isn't a service then the sc batch file method won't work either, you'd have to do some parsing on output of tasklist.exe to see if it is in memory then re-run the app. if not.

Steve
0
 

Author Comment

by:dsa_erie
ID: 18792430
Here is my situation.  I run Kerio Mail Server with ClamAV.  I've tried to get ClamAV installed as a service, but it's not working for me.  That's ok.  Once I have this batch file in order all will be fine.  The number 1 issue I have with these two programs is the order in which they are started.  ClamAV need to be started before KMS.  The problem with ClamAV is that it sometimes takes two or three attempts before it starts properly.  So I need to know if it has started via the batch file.  Same thing for KMS.  I hope this clears it up a bit.
0
Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

 
LVL 43

Expert Comment

by:Steve Knight
ID: 18792534
OK, how do you start the apps... are you just running the EXE then?  Is this on 2003 in which case we have tasklist.exe available otherwise we'll have to use resource kit commands.

Sounds like great applications to work with!

Steve
0
 
LVL 30

Accepted Solution

by:
SteveGTR earned 250 total points
ID: 18792608
Try this. It starts the first service and waits for it to start. Then it starts the second service. You may change the service1 and service2 variable to reflect the service name as reported by sc query.

@echo off

setlocal

set service1=ClamAV
set service2=KMS

sc start "%service1%"

REM ** Wait for service 1 to start

:CHECKAGAIN

for /f "tokens=1,* delims=:" %%a in ('sc query "TermService" ^| findstr RUNNING') do goto CHECKAGAIN

sc start "%service2"
0
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 250 total points
ID: 18793167
If it is 2003 you could do something like this, could be made more elegant but is this the sort of thing you are after?

@echo off
set service1=clamav.exe
set path1=c:\program files\clamav
set service2=kms.exe
set path2=c:\program files\kms
set maxtries=5
set /a count=0

:service1
tasklist | find /i "%service1%"
if "%errorlevel%"=="0" echo %service1% is running & goto service2
if %count%==%maxtries% echo Unable to start %service1% & goto end
set /a count=%count% + 1

rem re-run service #1
%path1%\%service1%

rem wait 10 seconds for it to start or fail presumably
ping 127.0.0.1 -n 10 >NUL

rem Try again
goto service1

:Service2
%path2%\%service2%

:end
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 18949815
I'd be happy with a split with SteveGTR... although the Q talks of services it turned out to be normal EXE files, not services which is why I offered a solution checking it is running with tasklist.exe

Steve
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 18949942
I would agree to that :)
0
 
LVL 1

Expert Comment

by:Computer101
ID: 20482853
Forced accept.

Computer101
EE Admin
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question