Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remotely Restarting a Service Reliably

Status
Not open for further replies.

humbletech99

Programmer
Nov 22, 2005
155
0
0
GB
I need to be able to reliably and remotely restart a service from cmd/batch.

So far I have been doing
Code:
sc \\server stop servicename
sc \\server start servicename
But because of a timing issue this invariably leaves the service as just stopped. So I introduced a time wait between the two commands, but this doesn't always work, if the server is slow to respond to the first one, it's not ready by the time of the second one and the service never restarts.

Anyone got any good reliable method for restarting a service and making sure it comes back up from cmd/batch?
 
I think a bunch of batch logic should really be avoided. I would just schedule a batch to run every minute, or ten depending on your needs. Have the regularly scheduled batch send a service start command. Sending the start command when the service is already running shouldn't be a problem.

However, why haven't you just used the restart command, instead of 2 separate stop and start commands?
 
I don't think sc has a restart command, only start and stop.

I've tried restart but it didn't accept it, gave me usage help instead.

I can't have this this just scheduled to start all the time, it's for a batch to be used on one off occasions to deploy some files and restart a service...
 
Well, there's an old addage about what happens when you assume. I did assume there was a restart, my bad. I found a link here...


that says,

PsTools from SysInternals comes with psservice.exe which has a 'restart' command.

I've not used PSTools in awhile, but I suspect they're where yo want to go. Since they're Microsoft owned now, they're not as trustworthy as before... har har... but they still have the PsService command in PsTools, and it is still, amazingly, free.

 
You could use a loop (a goto label and goto command) along with grep and sc \\servername query to see if the service is stopped.

Denny
MCSA (2003) / MCDBA (SQL 2000)
MCTS (SQL 2005 / Microsoft Windows SharePoint Services 3.0: Configuration / Microsoft Office SharePoint Server 2007: Configuration)
MCITP Database Administrator (SQL 2005) / Database Developer (SQL 2005)

My Blog
 
those are both good ideas...

The problem with the pstools idea is that it's not portable, you need to install pstools whereas I prefer to stick to readily available default tools for simplicity.
 
What you will need to do then is goto a vbscript. In vbs you would use WMI to query the service status and issue the start.
 
does this actually solve the problem with a restart command or are you actually talking about a start command which brings us back to square one...

if you issue a start command before the stop has completed, it won't work, the service will stop later and stay stopped... hence the whole discussion...
 
Here is the technet script center repository that has examples for service control:


These examples will show you how to setup the WMI query and what the properties are that you can access. You will see the methods to call to stop and start the service.

You would replace your batch file entirely. The sc stop becomes a call to the StopService method. You can then use a loop to query the status of the service. Once the status changes to "Stopped" you can exit the loop and do any other processing you want while the service is stopped. Finally issue a call to StartService. You could do another loop to verify that it does really start.

If you need to put this into Windows task scheduler, you would use cscript <your-script>.vbs as the command line in the scheduled task.

Browse over to the tek-tips forum on vbscript if you need more help. A google search on "win32_service" will get you some good results also.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top