Restart a windows service from command line

Roberto Madro R.
Roberto Madro R. used Ask the Experts™
on
I'm familiar with the "net start", "net stop", etc. to manage a service in windows, but do you know of a "restart" command?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Alan HardistyCo-Owner
Top Expert 2011

Commented:
You can combine the two into one command to restart a service:

net stop "Service Name" && net start "Service Name"

Not aware of a restart command line command.

http://technet.microsoft.com/en-us/library/cc736564(v=ws.10).aspx
Roberto Madro R.Programmer Analyst

Author

Commented:
On a service that's behaving normally your suggestion works great, my problem is that I have a 3rd party app that has a deffective service that hangs between start and stop, so I can only restart (as the only logical step remaining), I can't stop the service because according to its status it's already "stopped" or sort-of stopped, and I can't restart it because it's already "sarted" or sort-of started, that's why I'm left with the "Restart" option. And I'm thinking since windows offers the "Restart" command, it must be known / available for use from the command line.
You can script this
Step 1 find the name of the service and query svcname
take the PID from the step and then run “taskkill /PID xxxx /F”

you can write this into a simple bat file and pass in the service name
11/26 Forrester Webinar: Savings for Enterprise

How can your organization benefit from savings just by replacing your legacy backup solutions with Acronis' #CyberProtection? Join Forrester's Joe Branca and Ryan Davis from Acronis live as they explain how you can too.

Co-Owner
Top Expert 2011
Commented:
You should be able to use the taskkill command to terminate a hung service:

So combined:

net stop "ServiceName"
taskkill /f /fi "services eq ServiceName"
net start "ServiceName"
Roberto Madro R.Programmer Analyst

Author

Commented:
EE pros come through again, many thanks, worked like charm.
Roberto Madro R.Programmer Analyst

Author

Commented:
Can I follow up?  Is that allowed.?  if so, how can execute this against a remote server?

Thx
Alan HardistyCo-Owner
Top Expert 2011

Commented:
Use the following:

sc \\computername stop "ServiceName"
taskkill /s computername /f /fi "services eq ServiceName"
sc \\computername start "ServiceName"

Technically you should only ask one question per thread, but it's the season of goodwill :)
Using Alan's method you can use psexec

psexec \\servername /cmd /c net stop "ServiceName"
psexec \\servername /cmd /c taskkill /f /fi "services eq ServiceName"
psexec \\servername /cmd /c net start "ServiceName"
Roberto Madro R.Programmer Analyst

Author

Commented:
You mighty kind Alan, Many thanks to both of you & happy new year.
Alan HardistyCo-Owner
Top Expert 2011

Commented:
Anytime.

Happy 2013 :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial