checking a pid inside java

W

Wil

I want to check the pid of a different program using java if possible.

Basically I need to know if another process or application is running
on the same machine and when it stops running. I would like to check
it's status every few seconds or so. The java program would be passed
the process id at startup.

I don't know if this is possible to do in java. I might have to use
the native interface or something else.

Thank you,
Wil
 
A

Anthony Borla

Wil said:
I want to check the pid of a different program using
java if possible.

Basically I need to know if another process or application
is running on the same machine and when it stops running.
I would like to check it's status every few seconds or so.
The java program would be passed the process id at startup.

I don't know if this is possible to do in java. I might have
to use the native interface or something else.

Your use of the term 'pid' suggests you are on a *NIX / Linux platform. May
I suggest a quick, JNI-free [but ugly, and grossly inefficient] solution:

* Use 'Runtime.exec' to launch 'ps' [or similar utility] to
obtain the 'pid'

* You could use a Timer object for controlling the duration
of this periodic 'pid check'

A JNI-based approach would, of course, be far more robust.

I hope this helps.

Anthony Borla
 
S

Steve W. Jackson

:I want to check the pid of a different program using java if possible.
:
:Basically I need to know if another process or application is running
:eek:n the same machine and when it stops running. I would like to check
:it's status every few seconds or so. The java program would be passed
:the process id at startup.
:
:I don't know if this is possible to do in java. I might have to use
:the native interface or something else.
:
:Thank you,
:Wil

Assuming you're running only on some Unix flavor that has PIDs, you
could probably find a way to use the Runtime.exec() approach to execute
a form of the ps command that will get you info on the specified
process. More detail will of course warrant JNI, but if you just want
to determine if the process is running -- or even more about what's
offered by ps, this could work.

= Steve =
 
G

Gordon Beaton

I want to check the pid of a different program using java if
possible.

Basically I need to know if another process or application is
running on the same machine and when it stops running. I would like
to check it's status every few seconds or so. The java program would
be passed the process id at startup.

Do:

Process p = Runtime.getRuntime().exec("kill -0 " + pid);

then check p.exitCode(). Zero indicates that there is an existing
process (maybe yours, maybe not) with the specified pid.

/gordon
 
S

Steve W. Jackson

Gordon Beaton <[email protected]> said:
:On 18 Nov 2003 13:09:34 -0800, Wil wrote:
:> I want to check the pid of a different program using java if
:> possible.
:>
:> Basically I need to know if another process or application is
:> running on the same machine and when it stops running. I would like
:> to check it's status every few seconds or so. The java program would
:> be passed the process id at startup.
:
:Do:
:
: Process p = Runtime.getRuntime().exec("kill -0 " + pid);
:
:then check p.exitCode(). Zero indicates that there is an existing
:process (maybe yours, maybe not) with the specified pid.
:
:/gordon

Hmmm... The man page on my Mac OS X system doens't mention 0 as a valid
signal, but it works. Glad to know of this trick for more than just
Java use.

= Steve =
 
G

Gordon Beaton

Hmmm... The man page on my Mac OS X system doens't mention 0 as a
valid signal, but it works. Glad to know of this trick for more than
just Java use.

Not a trick. From the Single UNIX Specification:

If sig is 0 (the null signal), error checking is performed but no
signal is actually sent. The null signal can be used to check the
validity of pid.

/gordon
 
S

Steve W. Jackson

Gordon Beaton <[email protected]> said:
:On Wed, 19 Nov 2003 11:01:37 -0600, Steve W. Jackson wrote:
:> Hmmm... The man page on my Mac OS X system doens't mention 0 as a
:> valid signal, but it works. Glad to know of this trick for more than
:> just Java use.
:
:Not a trick. From the Single UNIX Specification:
:
: If sig is 0 (the null signal), error checking is performed but no
: signal is actually sent. The null signal can be used to check the
: validity of pid.
:
:/gordon

I don't doubt its source, only that the man pages for kill don't mention
it on two systems to which I have immediate access. Otherwise, I
would've know about it years ago. Either way, nice to have now.

= Steve =
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,082
Latest member
KetonaraKetoACV

Latest Threads

Top