0

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.

George
  • 500
  • 4
  • 19
  • 40
  • Yes, this is well documented on the internet. http://stackoverflow.com/a/133926/428779 – gWaldo Sep 07 '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 07 '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 07 '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 07 '12 at 16:19
  • George: the event viewer is there to log events, like the event that a service restarted. If you want a log file, that rests with the service executable itself. The native way described by TheCleaner is the best way to handle this on a Windows system. – brandeded Sep 07 '12 at 16:30
  • @George see my answer then below – TheCleaner Sep 07 '12 at 16:31
  • 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 07 '12 at 16:51

5 Answers5

1

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.

Steve G
  • 231
  • 1
  • 11
1

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.

Andrew S.
  • 26
  • 1
0

Try this. It will restart the service if it's already running. Otherwise, will start it.

sc stop "MyService" && sc start "MyService" || sc start "MyService"
Sansei
  • 1
0

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.

TheCleaner
  • 32,627
  • 26
  • 132
  • 191
  • 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
0

Sc command.

Try sc/? For help.

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

Tonny
  • 6,332
  • 1
  • 18
  • 31