File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes How does one get a PID in Java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How does one get a PID in Java?" Watch "How does one get a PID in Java?" New topic
Author

How does one get a PID in Java?

Gabriel Perry
Greenhorn

Joined: Jul 26, 2004
Posts: 4
obviously, this is a platform specific question, i'm running a java app on a unix/linux platform and want the store the PID in a kind of lock file. does one know how to do this?

i searched around cyberland a bit and read that one can use Runtime.exec() to execute the following perl command...

perl -e 'print getppid()."\n";'

but i'm wondering if there is a better way to do this. for instance, can one use some of the capabilities in the reflection API?
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

For which reason do you need the pid? Perhaps there is a more java-like way to solve your problem - more platform independent.


http://home.arcor.de/hirnstrom/bewerbung
Gabriel Perry
Greenhorn

Joined: Jul 26, 2004
Posts: 4
i've got a cron job set to call this java app every minute. this app looks in a directory for some files, if it finds the files it's looking for then it does some processing on them. if it doesn't find the files, it quits.

i need a way to prevent other java processes from interferring with this, uh, process. so the initial java app looks to see if a lock file exists in the search directory. if it doesn't then it creates one and stores its PID in it.

why? because if another java process comes in and sees the lock file, it will get the stored PID and look to see if it exists in /proc if it does, it knows to get out, if it doesn't find the PID in /proc, then it knows that it can ignore the lock file, that is, the lockfile is bad (leftover from a process that is no longer running.)

does this give you a reasonable idea what i'm trying to accomplish?
James Swan
Ranch Hand

Joined: Jun 26, 2001
Posts: 403
Could you capture that info when you launch your java process?

eg. create a script for starting the process and have it do somthing like

Gabriel Perry
Greenhorn

Joined: Jul 26, 2004
Posts: 4
yea, i suppose, but i'm wondering if there is a way to do this from within java and not using Runtime.exec() (not running a shell or perl)
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

You may open a socket on an used port, and if it fails, (because the port is in use), you don't start.

As far as I know, a terminated programm will always release the lock.

Another solution:

$$ will expand to the PID, so your Programm get's started

for example, and may store the '1642' in a lockfile.
Now - if the program terminates abnormal, without removing the lock-file, the next starting will show: there is a lock-file. Your newly started program shall now find out, whether this lockfile is valid:
a) is there a process running with this pid?
b) is this really your 'myProg'?

a) isn't a common java-task, but you could look at /proc/PID to see if there is a file with this pid.
b) could be done in a similar way.

I wouldn't solve it from within java (and of course not with Runtime - but that wasn't the suggestion of James), but from the script.
In crontab call the script:
.... filemod.sh

in the filemod.sh:

[ July 26, 2004: Message edited by: Stefan Wagner ]
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

Ah - I made a mistake.
If the program tests itself for running, it is of course running, so 2 would be the testing program (plus headline), 3 would mean another instance running, 4 and above: serious problem.

Or you start another script inside the script, which starts the java-program, and query for that in this testscript.
Gabriel Perry
Greenhorn

Joined: Jul 26, 2004
Posts: 4
thanks for your help. i'll give these suggestions a try.

g
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How does one get a PID in Java?