Restarting of the service application from the service application itself

A

Andreas

Hi everybody,

Can anyone advise on how to restart service application from the application
itself? I've tried lunching second instance of the same application (as
normal application, not a service) with predefined parameter, which has to
open SCM, stop the service, start it and then exit, but apparently SCM
detects that another instance of the application is already running and
tries to use it directly. As a result StartService returns
ERROR_SERVICE_REQUEST_TIMEOUT which I presume happens because its main
thread is already blocked with the call to StartService. Will doing this
from the separate thread help? Won't there be any other restrictions? Has
anyone tried this and can possibly give some directions?

Thanks in advance,
Andreas
 
L

LiquidJ

Never tried this, but why would you want to? Wouldn't doing an internal
"restart" of the service when it needs to be restarted be much more
efficient? Why get the SCM involved?


{L}
 
A

Andreas

Hi,

For example when there is memory leak and in the part you can not change. We
need to restart the service in such case and want to have it started in new
process. Another reason is that client might want to restart it from
client's side and we want to avoid dependency on other application specially
created and redistributed only to be able to restart this service.

Andreas
 
L

LiquidJ

Fair enough.

At any rate, I don't see any reason why this should fail, other than the
following possibilities:
- After calling ControlService(..., SERVICE_CONTROL_STOP, ...), you didn't
wait for the service to stop. ControlService() returns when the service has
been -=instructed=- to shut down - not when it has actually shut down. You
should query the status of the service until it return SERVICE_STOPPED
before trying to start it again.
- You try to restart the service after registering the handler.

What I've done is to create a barebones service, which does what you're
trying to do. Everything seems to work. I can send you the code if you
like. Basically, the algorithm is this:


Check for command line parameters
if "restart" is on the command line then
Open the SCM
Stop the Service
Do
Query the service
until SERVICE_STOPPED
Start the service
else
Register handler
Do service things
endif



{L}
 
M

Martin Stephenson

You could probably shut down the service from within itself, but how to
restart it .....

I'd probably script a shutdown/restart which ran as a seperate process, call
that script from within the service app to perform the shut down then
restart. remember to call the script from within the service app with a
nowait to prevent the service app from not shutting down, but waiting for
the call to complete instead.

If you wanted to get clever and allow external clients to shut down the
service without giving the clients any privileges on the server then write a
specific restart service app which uses a TCP/IP socket and a specific port
to accept inbound connections. This can be accessed from anywhere and
shutdown/restart commands sent to the service using simple text. The
restart service app simply sits there waiting for command with an IP port
open. I've used something similar to allow remote access to perform
privileged operations on a server. bear in mind it's not very secure
(unless you build in security) but in my case it didnt matter. I also wrote
a similar app to allow me to remotely open and close someones CD drawer,
drove them mad for weeks :blush:)

Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top