Solved

Check if a service or process is running.

Posted on 2007-03-25
11
953 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

828 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