El_Nombre Posted October 21, 2010 Posted October 21, 2010 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? :\
mattx Posted October 21, 2010 Posted October 21, 2010 (edited) 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: @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. Edited October 21, 2010 by mattx 1
El_Nombre Posted October 21, 2010 Author Posted October 21, 2010 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.
featured_spectre Posted October 21, 2010 Posted October 21, 2010 Damn you matt you beat me to it! grrr
mattx Posted October 21, 2010 Posted October 21, 2010 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.
sted Posted October 21, 2010 Posted October 21, 2010 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?
srochford Posted October 21, 2010 Posted October 21, 2010 Loads of suggestions already but why not add a couple more :-) 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!)
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now