The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Make Windows Service restart itself

Do any of you know if this is possible from within the service itself? I know I can hack something together, like creating a batch file with "net stop service" and "net start service" and calling that with shellexecute, but do you know if there is any way to do it reasonably cleanly from within the service itself?
Friday, February 10, 2006
Making it restart itself from within itself could be tricky if not impossible.  If it were me I would make another small exe that calls ControlService (using SERVICE_CONTROL_STOP), waits to make sure it is closed and then call StartService to start it up again.  Then have your service call the exe.  Since the service will presumably be running with proper execute priviledges it should be able to launch the exe with proper credentials.
Friday, February 10, 2006
It will need to have a separate service watching to start and stop it. Most of the time when it looks "stopped" it really has a dialog box popped up on its own invisible and inaccessible desktop.
Friday, February 10, 2006
Bacon is correct. The only way to do it is to create a 'stub' program that simply stops and starts your service. It should actually be very simple to do.
Steve Wiseman Send private email
Friday, February 10, 2006
I think the common pattern is to create a small service control program as either a service itself or a tray icon app, if it will be accessed by the user.
MBJ Send private email
Friday, February 10, 2006
I've worked with services and I can tell you that's it's impossible. Just use a small control utility that will do the equivalent of a "net stop service" and "net start service".

If there really is a way to do this, then I would love to find out what's required to pull it off.
QADude Send private email
Friday, February 10, 2006
Why do you need to do this?

Almost all of the answers that I can think of suggest that you should fix the REAL problem with the service rather than resort to the tired old anti-pattern of the auto resurecting daemon process...

But it's late and I've been drinking a nice bottle of wine so I could be missing something...
Len Holgate Send private email
Friday, February 10, 2006
+1 to Len (even if he's slightly tipsy!)

Fix the original problem, don't apply a band-aid.
example Send private email
Friday, February 10, 2006
What if the "real problem" is that the service needs to be replaced with another binary (updated version of itself)?

Friday, February 10, 2006
Then I'd structure the system so that there was a "service that did the work" and a "service that checked and applied updates". The update service could then control and replace the worker service.

IMHO anything else is poor design (clumping unrelated functionality together for the sake of it; what if you didn't want to run the update service?) But see previous state of mind/wine caveats... ;)
Len Holgate Send private email
Friday, February 10, 2006
If the service needs to update itself it can write a setup executable and run that process, and have the setup executable shut down the service, replace it, and restart it.
Friday, February 10, 2006
ok it's the hack of a lifetime, but it can be done. 

1. Configure the service recovery options so that it is restarted automatically.

2. When you want to restart the service, have the service exit "unexpectedly."
KenE Send private email
Monday, February 13, 2006
If it is just to "reset" or "apply new config" just call your "stopservice" and "startservice" routines internally yourself. no big deal. If you can't do that, redesign your service. The signal to do this can either come from an external app or from something that is inside your service (like providing an http interface or whatever).

If you want to exchange the service binary: build a MSI and let the user/admin execute it. the windows installer service will take care of stop/replace/restart.
Thursday, February 16, 2006

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
Powered by FogBugz