Solved

Check if a service or process is running.

Posted on 2007-03-25
11
919 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
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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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…
One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now