Solved

Check if a service or process is running.

Posted on 2007-03-25
11
930 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
push same scheduled task across multiple servers? 16 140
Convert JPG to PDF file 4 86
Batch: check service status 11 86
For loop to add numbers 3 59
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
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…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

947 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

22 Experts available now in Live!

Get 1:1 Help Now