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]
# 1
Easiest way is to open a ServerSocket on some fixed port. If you succeed, OK: if you don't, you are not alone.
ejpa at 2007-7-14 20:42:12 > top of Java-index,Java Essentials,Java Programming...
# 2

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.

JohnKeltya at 2007-7-14 20:42:12 > top of Java-index,Java Essentials,Java Programming...
# 3
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 > top of Java-index,Java Essentials,Java Programming...
# 4
So slow...
jverda at 2007-7-14 20:42:12 > top of Java-index,Java Essentials,Java Programming...
# 5
haha
ejpa at 2007-7-14 20:42:12 > top of Java-index,Java Essentials,Java Programming...