Check service is running with procrun

738 views Asked by At

I'm using prunsrv to create windows services. I would like to ask you if it is possible to query the status of a process (if it running, stopped) or if a process is existing. Also I would like to read the content of the pid file with a Java program.

1

There are 1 answers

0
Aaron Digulla On BEST ANSWER

Yes, with a trick: Create a local socket in the start up code of the service. If you get an error creating the socket, you'll know that an instance of the service is already running.

If you want to check that the service is alive, you can connect to the socket. If that fails, the process isn't there.

And you could return the PID of the service via this socket, so the "monitor" program wouldn't need to know the log path and PID file name. And if you implement the start/stop methods, you can even tell the monitor the status of your service.

The biggest advantage of this is that the OS will make sure that the socket is closed when your service terminates for any reason. The second advantage is that this works independent of the OS - if you ever need to move this to Linux or Mac, no changes to the code will be necessary.