Checking for duplicate instances of java program.
Hi Guys,
I've got one of my java programs running on a linux box via a cron process every XX minutes.
Now, I want to be able to somehow check (when the java program is launched) whether or not another instance of this class (program) is already running on this system.
I can I can dump data from "top" and "pse" into a variable, parse it and look for stuff there, but I was hoping there would be some type of a built in solution.
If this process is already running, the application should quit, otherwise it should run.
Thanks
Lucy
[579 byte] By [
Lucy83a] at [2007-10-3 2:53:08]

No, nothing is built in to help with that, so you might be stuck with top or pse.
If you have access to the source code, you can stick in some sort of heartbeat functionality in a new separate thread where each running instance updates a file or database periodically to declare that it is currently running.
Open a ServerSocket on a fixed port. If it succeeds, you're the only instance running. If it fails, another instance is running (or some other app is using that port--so it's ipmortant to pick a port you know isn't used for something else).
jverda at 2007-7-14 20:42:12 >
