Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Daemon (starting / stopping)

Status
Not open for further replies.

klingoni

Technical User
Apr 30, 2002
5
0
0
CH
Hi everybody

I'm writing a daemon in java which is started from the console on several machines. Now, my question is, what is the easiest way to be able to start / stop daemons from the console. How can I find out if the daemon is already running (if, for example, someone wants to start it again) or how can I shut it down cleanly if it is running already? I'm thinking of something like this:

starting: [blue]java myDaemon start [additional args][/blue]
stopping: [blue]java myDaemon stop[/blue]

Somehow, I have to check if there already is a Daemon instance in the JVM when it is invoked with the start or stop argument. But I don't really know how to do this? I also don't know how to tear down (destruct) the deamon, if it is running and should be shut down??

Any help greatly appreciated

Sebastian
 
To prevent a second instance to be started, you could use a Lockfile (java.nio.channels.FileLock).
To send a stop-signal to an already running process, I would try to use a socket, or rmi (which should work on localhost too) - I didn't tried this.

seeking a job as java-programmer in Berlin:
 
I would also go for the socket appoach (maybe in combination with the lock). You could make a (small) http server to manage yout daemon(s) in the same way as you can configure / manage a router or any network attached device. Since your daemons are running on several machines, you can administer them centrally or from any "pc" with a web browser.
PS : The daemon and the http server are threads in the same process.
[Instead you could pop up a gui, but that would only work on the local machine.]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top