How do you do....it? Thread, Batch file for starting stopped services? in Technical; Hi folks - this will probably be a quick one.
I'm new to batch files, as in totally new. I'd ...
21st October 2010, 01:17 PM #1
Batch file for starting stopped services?
Hi folks - this will probably be a quick one.
I'm new to batch files, as in totally new. I'd like to run a batch file as a scheduled task every morning to check if a service on a server is running and if it's not, start it up.
The command to start eg print spooler runs fine as:
NET START "spooler"
And I'd assume the batch file to start the service if it's not running would be:
IF "spooler" == stopped NET START "spooler"
...But that might be nonsense. Computer definitely says no anyways.
What am I missing helpful edugeeks? :\
IDG Tech News
21st October 2010, 01:19 PM #2
Just have the net start - if it's already running it will continue to do so.
On checking mine - I have the print spooler stopping and starting each day:
Lordy - 2005 !! I've been here toooooooooo long. If it's already running you will get the:
echo * Daily Prog For Stopping and *
echo * Starting the Print Spooler. *
echo * *
echo * Matt June 2005
net stop "Print Spooler"
net start "Print Spooler"
The requested service has already been started.
Last edited by mattx; 21st October 2010 at 01:23 PM.
Thanks to mattx from:
El_Nombre (21st October 2010)
21st October 2010, 01:38 PM #3
Wow - that was a quick reply! Thanks mattx I'll set that going.
I'm all answered but, should the situation arise, how would I go about putting that 'if' condition into the batch file if I wanted to do things the 'long' way? Am I close? It's bugging me now you see.
Cheers for the help. I'm now filtering sites by IP addy so no more gerbils for me either.
21st October 2010, 01:42 PM #4
Damn you matt you beat me to it! grrr :P
21st October 2010, 03:06 PM #5
Well I stopped [ sort of ] writing stuff in batch a while ago - I tend to use AutoIt for most of my stuff - but to answer your question you would most probably have to use an 'If' with an Errorlevel, and then a GOTO and then it starts to get messy. For a better detailed breakdown type in If /? from the command line - see below:
Originally Posted by El_Nombre
Performs conditional processing in batch programs.
IF [NOT] ERRORLEVEL number command
IF [NOT] string1==string2 command
IF [NOT] EXIST filename command
NOT Specifies that Windows should carry out
the command only if the condition is false.
ERRORLEVEL number Specifies a true condition if the last program run
returned an exit code equal to or greater than the number
string1==string2 Specifies a true condition if the specified text strings
EXIST filename Specifies a true condition if the specified filename
command Specifies the command to carry out if the condition is
met. Command can be followed by ELSE command which
will execute the command after the ELSE keyword if the
specified condition is FALSE
The ELSE clause must occur on the same line as the command after the IF. For
IF EXIST filename. (
) ELSE (
echo filename. missing.
The following would NOT work because the del command needs to be terminated
by a newline:
IF EXIST filename. del filename. ELSE echo filename. missing
Nor would the following work, since the ELSE command must be on the same line
as the end of the IF command:
IF EXIST filename. del filename.
ELSE echo filename. missing
The following would work if you want it all on one line:
IF EXIST filename. (del filename.) ELSE echo filename. missing
If Command Extensions are enabled IF changes as follows:
IF [/I] string1 compare-op string2 command
IF CMDEXTVERSION number command
IF DEFINED variable command
where compare-op may be one of:
EQU - equal
NEQ - not equal
LSS - less than
LEQ - less than or equal
GTR - greater than
GEQ - greater than or equal
and the /I switch, if specified, says to do case insensitive string
compares. The /I switch can also be used on the string1==string2 form
of IF. These comparisons are generic, in that if both string1 and
string2 are both comprised of all numeric digits, then the strings are
converted to numbers and a numeric comparison is performed.
The CMDEXTVERSION conditional works just like ERRORLEVEL, except it is
comparing against an internal version number associated with the Command
Extensions. The first version is 1. It will be incremented by one when
significant enhancements are added to the Command Extensions.
CMDEXTVERSION conditional is never true when Command Extensions are
The DEFINED conditional works just like EXISTS except it takes an
environment variable name and returns true if the environment variable
%ERRORLEVEL% will expand into a string representation of
the current value of ERRORLEVEL, provided that there is not already
an environment variable with the name ERRORLEVEL, in which case you
will get its value instead. After running a program, the following
illustrates ERRORLEVEL use:
echo Program had return code 0
echo Program had return code 1
You can also using the numerical comparisons above:
IF %ERRORLEVEL% LEQ 1 goto okay
%CMDCMDLINE% will expand into the original command line passed to
CMD.EXE prior to any processing by CMD.EXE, provided that there is not
already an environment variable with the name CMDCMDLINE, in which case
you will get its value instead.
%CMDEXTVERSION% will expand into a string representation of the
current value of CMDEXTVERSION, provided that there is not already
an environment variable with the name CMDEXTVERSION, in which case you
will get its value instead.
21st October 2010, 08:10 PM #6
just to cut the code well back could you not just schedule a batch file of net start spooler every 5 mins if its running it will do nothing if its stopped it will start?
21st October 2010, 08:57 PM #7
Loads of suggestions already but why not add a couple more :-)
The sc command allows you to do "stuff" with services - in this case we query the status of the spooler service - try sc query spooler at a prompt to see what it does.
sc query spooler | find /i "running"
if errorlevel 1 (
echo not running
echo starting spooler
net start spooler
The results are piped (the | symbol) through the find command to see if the text running is there (the /i says "ignore case" so it finds it if it actually says RUNNING or whatever)
If the find command works (ie the service is running) then the errorlevel is zero (no error; find found what you told it to find) but if "running" was not found then errorlevel is set and the stuff inside the brackets happens.
The other alternative is to just use services.msc to set the spooler service to restart if it stops. If you look at the properties of any service you'll see there's a "recovery" tab - here you can tell the system what to do if a service stops. The simplest is to just restart the service and this is good for services which are normally fine but occasionally fall over. You can also do things like restarting the machine (drastic but might be the best solution for some things) or you can run a program. If that program was something like blat then it can send an email to you.
A reasonable set of options would be to make it just restart the service on the first and second failure but send an email on the third (because by then you probably need to do something!)
By fil_b in forum Windows Server 2000/2003
Last Post: 5th November 2009, 05:13 AM
By jussy65 in forum Scripts
Last Post: 13th August 2008, 08:10 AM
By beeswax in forum Scripts
Last Post: 3rd March 2006, 08:48 PM
By RichCowell in forum Windows
Last Post: 17th January 2006, 12:42 PM
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)