Script to check for a service and start???

  • Thread starter Thread starter ara
  • Start date Start date
A

ara

Hi,

I have a service on one of our XP PC's that keeps falling over. Until
it's fixed properly, I thought about creating a script that will run
every 5 mins to check if the service is running and start it if not.

Unfortunatly as I don't have the required knowedge I'm looking for
advice on where the script should be placed and also the contents.

Any assistance will be gratefully received.
 
You may get better ideas than this, but here goes.

First, this sounds like a band-aid kind of deal.

Second you need to know the Service Name as opposed to the Display Name.
More on that below.

Any of these commands work in the command prompt or a .bat or .cmd file,
commonly called Batch files or Scripts. .bat files will work on all Windows
versions and .cmd files will only work on Windows NT, like Windows XP (which
is NT 5.1) or Windows 2000 (NT 5.0), etc.

SC is a command line program used for communicating with the NT Service
Controller and services.

You can type: sc /? in a command prompt for help on the SC command.

You can also paste the following line into Start | Run and click OK...

hh ntcmds.chm::/sc.htm

for more Help on the SC command.

sc query servicename

For example..

lanmanserver is the Service Name for the Server Service (the Display Name).

sc query lanmanserver

will show if the Server service is running, stopped or paused.

But I think that we can skip the sc query bit.

The Net Start command starts a service.

net start servicename

For example..

net start lanmanserver

You could just have a .bat file with the following two lines...

@ echo off
net start servicename

If the service is already running you will get this message.

The requested service has already been started.

But the error will flash by so quick, you cannot even read it.

Open Notepad, add these two lines:


@ echo off
net start servicename


Replace servicename with the service's actual Service Name.

Save the file as Somename.bat

Then you can just double click Somename.bat whenever to run it or you can
probably schedule it to run every 5 minutes or something.

Services - Service Name & Display Name

To find the Service Name.

There are several ways to do this. Here are three that I know of.

Open Services...
Start | Run | Type: services.msc | Click OK |
Scroll down to and double click the service you want to stop |
On the General tab, Service name: take note of the Service Name not the
Display Name | Close Services

Look in...
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services

The command:

sc query state= all

will display the SERVICE_NAME and DISPLAY_NAME of all services & drivers.

Any commands using the command prompt will require using the Service Name
not the Display Name.

I.e. Net Stop, Set Start, SC commands, etc.

--
Hope this helps. Let us know.

Wes
MS-MVP Windows Shell/User

In
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top