Restart a Windows service from a script?
Solution 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.
Solution 2
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.
Related videos on Youtube
George
Updated on September 18, 2022Comments
-
George over 1 year
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
andnet 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.-
gWaldo over 11 yearsYes, this is well documented on the internet. stackoverflow.com/a/133926/428779
-
kralyk over 11 yearsIt'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.
-
George over 11 years@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 over 11 years@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.
-
mbrownnyc over 11 yearsGeorge: 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.
-
kralyk over 11 years@George see my answer then below
-
Florian Haider over 11 yearsI'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.
-
-
George over 11 yearsThanks! 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...