+ Post New Thread
Results 1 to 7 of 7
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 ...
  1. #1
    El_Nombre's Avatar
    Join Date
    Sep 2008
    Posts
    288
    Thank Post
    63
    Thanked 25 Times in 19 Posts
    Rep Power
    18

    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? :\

  2. #2

    mattx's Avatar
    Join Date
    Jan 2007
    Posts
    9,248
    Thank Post
    1,059
    Thanked 1,069 Times in 625 Posts
    Rep Power
    740
    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:

    Code:
    @Echo off
    
    echo *****************************************
    echo * Daily Prog For Stopping and           *
    echo * Starting the Print Spooler.           *
    echo *                                       *
    echo * Matt  June 2005                  
    echo *****************************************
    
    net stop "Print Spooler"
    net start "Print Spooler"
    Lordy - 2005 !! I've been here toooooooooo long. If it's already running you will get the:

    The requested service has already been started.

    Message.
    Last edited by mattx; 21st October 2010 at 01:23 PM.

  3. Thanks to mattx from:

    El_Nombre (21st October 2010)

  4. #3
    El_Nombre's Avatar
    Join Date
    Sep 2008
    Posts
    288
    Thank Post
    63
    Thanked 25 Times in 19 Posts
    Rep Power
    18
    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.

  5. #4

    nephilim's Avatar
    Join Date
    Nov 2008
    Location
    Dunstable
    Posts
    12,426
    Thank Post
    1,682
    Thanked 2,036 Times in 1,481 Posts
    Blog Entries
    2
    Rep Power
    460
    Damn you matt you beat me to it! grrr :P

  6. #5

    mattx's Avatar
    Join Date
    Jan 2007
    Posts
    9,248
    Thank Post
    1,059
    Thanked 1,069 Times in 625 Posts
    Rep Power
    740
    Quote Originally Posted by El_Nombre View Post
    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.
    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:

    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
    specified.

    string1==string2 Specifies a true condition if the specified text strings
    match.

    EXIST filename Specifies a true condition if the specified filename
    exists.

    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
    example:

    IF EXIST filename. (
    del 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
    disabled.

    The DEFINED conditional works just like EXISTS except it takes an
    environment variable name and returns true if the environment variable
    is defined.

    %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:

    goto answer%ERRORLEVEL%
    :answer0
    echo Program had return code 0
    :answer1
    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.

  7. #6


    Join Date
    Mar 2009
    Location
    Leeds
    Posts
    6,925
    Thank Post
    231
    Thanked 901 Times in 776 Posts
    Rep Power
    303
    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?

  8. #7

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,157
    Thank Post
    116
    Thanked 529 Times in 452 Posts
    Blog Entries
    2
    Rep Power
    124
    Loads of suggestions already but why not add a couple more :-)

    Code:
    sc query spooler | find /i "running"
    if errorlevel 1 (
    echo not running
    echo starting spooler
    net start spooler
    )
    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.
    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!)



SHARE:
+ Post New Thread

Similar Threads

  1. Batch File (select random file) help
    By fil_b in forum Windows Server 2000/2003
    Replies: 5
    Last Post: 5th November 2009, 05:13 AM
  2. batch file comman
    By jussy65 in forum Scripts
    Replies: 3
    Last Post: 13th August 2008, 08:10 AM
  3. batch file help
    By beeswax in forum Scripts
    Replies: 9
    Last Post: 3rd March 2006, 08:48 PM
  4. Services Not Starting...
    By RichCowell in forum Windows
    Replies: 6
    Last Post: 17th January 2006, 12:42 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •