Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Check if a service or process is running.

Posted on 2007-03-25
11
Medium Priority
?
987 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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
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 1000 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 1000 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

650 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