Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I know there are several similar posts on the subject, but what I would like to know is how to re-start a service that has already been stopped. Is there a .bat or .vbs script that would check if the service is stop and then restart it if it's stopped and run in a loop?

Thank you for your help!

Update 7.IX.2012 @ 11.25 I understand there are several posts that go into details on usage of SC and net stop & net start, most of those posts are dealing with stopping the service first and then restarting it. I am dealing with the service that already either crashed or stopped and I need to check if it is stopped and then restart it. I hope I am making it clearer. It would also be nice to have a log file attached to it too.

share|improve this question
    
Yes, this is well documented on the internet. stackoverflow.com/a/133926/428779 –  gWaldo Sep 7 '12 at 16:13
1  
It's pretty unnecessary...you can simply configure the service itself to restart on its Recovery tab if all you are wanting to do is start a service that gets stopped. –  TheCleaner Sep 7 '12 at 16:14
    
@gWaldoIt's stop the service first... I don't need to stop it first, I need to check if the service is already stopped and then restart it... –  George Sep 7 '12 at 16:16
    
@TheCleaner I kind of would like a log file too to go with that to see when it restarted. I know I can go through Event Viewer, but I need a quick access to a very specific service. –  George Sep 7 '12 at 16:19
1  
I'd argue that you should look into why the service is stopping/crashing and prevent it from happening in the first place rather than put a monitor in place to watch it and start it again when it crashes/stops. –  August Sep 7 '12 at 16:51
show 2 more comments

4 Answers 4

up vote 1 down vote accepted

As an example, create a batch file C:\derp.bat

Contents of batch file could look something like this:

net start "Service Name"

C:\derp.bat

First line: If the service is already running then nothing will happen. If it's not running then it will attempt to start.

Second line: restarts the batch file.

You could throw a ping 127.0.0.1 -n 60 in the middle if you wanted it to try every 60 seconds.

share|improve this answer
add comment

Starting a service in PowerShell is as easy as (using spooler as an example):

Start-Service spooler

You can also check if a service is running and have it restarted if it's not via:

$Service = Get-Service -Name spooler
if ($Service.Status -ne "Running")
{         
    (Get-Date).ToString() + " - Service stopped." >> C:\Scripts\log.txt
    Start-Service spooler
}

However, I wouldn't recommend looping a script infinitely. A quick and dirty solution would be to set this script to run every so often with a Scheduled Task.

Edit: Added a line to add an entry to a log file.

share|improve this answer
add comment

From what it sounds like you are asking after reading your comments, just get this and be done:

http://www.netwrix.com/windows_services_monitoring_freeware.html

NetWrix Service Monitor is a free tool to monitor critical Windows services and optionally restart them after failure. The tool monitors all automatic startup services on multiple servers at a time and sends e-mail alerts when one or more services stops unexpectedly. The optional automatic restart feature ensures that all monitored services are up and running without downtime. The tool is lightweight and very easy to configure: install, enter computer names, and supply your e-mail address.

Features and benefits:

One installation watches multiple computers at a time;
Monitors all Windows services with startup type set to Automatic;
Sends alerts by e-mail when services stop or fail to start at boot time;
Optionally starts failed services;
Optionally reboots computers when one or more services fail;
Available at absolutely no cost.

We use WUG, others use NAGIOS, Zenoss, whatever...but the above is free and specific to your question.

share|improve this answer
    
Thanks! I think it's a slight overkill for what I was looking for. I wish we would use Nagois here, this would simplify my life so much... –  George Sep 10 '12 at 21:46
add comment

Sc command.

Try sc/? For help.

It can check status of service. Based on that you can (re-)start if needed.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.