views:

94

answers:

4

I have a java application where I want to check to see if an application is running. If it is not running, I want to start it. If it is running, I want to kill it and then restart it.

Can someone tell me how to do this? I can start/stop the program easily enough, with the ProcessBuilder. But I cannot detect a process that is already running.

Thanks for your help! John

A: 

Might sound silly, but you can create a file with a known name on application startup. Doesn't need to contain anything. To check if your application is running, check if that file exists.

Not a good idea - when the application or system crashes, you'll have the file lying around indefinitely without the application running.
Michael Borgwardt
You are absolutely right about that.
Which is why you write the pid of the process to that file, so you can check if the process is actually running when reading that file. If there's no process with that pid, clean up the file. If there is, but it has a different name or something, clean up that file. Else, refuse to run. It's not foolproof, but it's a very common solution to this very problem.
wds
+1  A: 

Without the cooperation of the application (ideally have it listening on a network port), that may be impossible (your Java app might not have the rights to kill the app) and requires OS-specific code. On Linux, you'd uase the ps and kill commands.

Michael Borgwardt
Isn't there any other IPC mechanism for Java?
Ravi Wallau
No. Not in the standard API.
Michael Borgwardt
A: 

Is the other application (the process you are monitoring) under your responsibility? If so, you can use a method I used in some high-availability system a few years back: Open a connection to that other application and "ping" it. If the other application does not respond within a given timeout, it is either down or "hung", which is as bad (and something you can't detect through process monitoring. Opening a connection can be done using sockets, or though more sophisticated protocols (SOAP?). An alternative is to have the application send a "I'm alive" message every so often. If you haven't received it in some time - your application needs restarting. In order to actually "kill" the other process, you can keep the Process instance you get from the exec() method, and destroy() it when you so choose.

Armadillo
A: 

Thanks for the replies. That was what I was afraid off. We are trying NOT to add more things to the application that I want to start up. We are basically trying to add a remote control web interface to a collection of applications.

The web server application that I am writing would basically start/stop 3 apps that all talk to each other to achieve a goal. If my web server starts and stops them, all is well. But if, for some reason they are already running when I try to start them bad things happen.

It is something I know I could handle with Visual Studio (C++/C#/etc). But this project has to be written in java due to a platform independence requirement.

Thanks for your help everyone!