Python restart windows service
Learn more. How do I restart a Windows service from a script? Ask Question. Asked 12 years, 7 months ago. Active 1 year, 3 months ago. Viewed k times. I have a batch script that looks like: sc stop myservice sc start myservice it errors out because sc doesn't wait till the service is stopped.
Improve this question. Joshua Joshua 1 1 gold badge 9 9 silver badges 18 18 bronze badges. Add a comment. Active Oldest Votes. Improve this answer. Nice use of ping as a time delay. Thanks, there's no sleep in batch so that's all you can do to wait : — crb. Awarded to those that use a GOTO statement with a straight face. It's trivial in C , really!
In Windows Server , the error level is 0 when found and 1 when not found. So I had to invert the logic and then it worked. Show 6 more comments. Just create a loop based on your running condition 21 4. Enjoy 23 ''' 24 25 import socket 26 27 import win32serviceutil 28 29 import servicemanager 30 import win32event 31 import win32service 32 33 34 class SMWinservice win32serviceutil. CreateEvent None , 0 , 0 , None 54 socket. ReportServiceStatus win32service.
SetEvent self. LogMsg servicemanager. Menu TOC share Top. See more: C. Can I get my Windows Service to restart itself? Posted Nov am MatthysDT. Add a Solution. Top Rated Most Recent. Accept Solution Reject Solution. Have you tried: C. Copy Code. Posted Nov pm Russ-T. Best way to have a Windows service restart itself is to set up two things: 1 Go to Services and look at the properties of your installed service. Set the Reset fail count after text box to 0 if you want to be able to restart it multiple times in a day.
Set the Restart service after text box to how long you want to wait. Now if your service fails for whatever reason, it will try to restart. Exiting with an error code should do it. ExitCode ref Now your Windows service will tell the service provider that it failed and will restart. Why would anyone do this, some may ask? Performance counters need time to configure. So a Windows service can create them on-the-fly, restart, and then use them. Other than that example, I'm sure there are other reasons.
Step 1 can be replaced with running a program or even restarting the computer. I prefer to run a program that reports the error to me and then restarts the service if X number of fail counts is less than Y. I run my program in the subsequent failures. Lastly you can configure the Recovery settings during installation of your service. There is a CodeProject article on it.
Someone here suggested scheduling a task to have the service start say, in 30 seconds in the future , then have the service stop itself. Posted Nov pm Abhishek Sur. You can also set the recovery attributes using a shell cmd. Posted Apr am rgonzruiz. Please don't answer questions that are 2 years old, especially if the OP has selected a correct answer already.
MatthysDT Apr am.
0コメント