Checking to see if PID is running

H

H. Wade Minter

Is there a preferred module, under Linux, to verify whether a given process
ID number is still running? Nothing looked promising via a CPAN search, and
I didn't get anywhere with "perldoc -q process" or "perldoc -q pid".

There were a few modules dealing with pid files, but nothing obvious if
you just had the pid number in a scalar and needed to check it.

Thanks,
Wade
 
A

A. Sinan Unur

Is there a preferred module, under Linux, to verify whether a given
process ID number is still running? Nothing looked promising via a
CPAN search, and I didn't get anywhere with "perldoc -q process" or
"perldoc -q pid".

perldoc -f kill

Sinan
 
N

Nathan Wagner

Is there a preferred module, under Linux, to verify whether a given process
ID number is still running?

You don't need a module. kill 0, $pid is what you're looking for.

There are security considerations that may make the result of this unreliable,
but it is unlikely that they will apply.

Given that you asked this particular question, I recommend that you read, in
addition to the perldoc on kill, the section 7 man page on signal, and up on
IPC in general. There are a few situations that you want to do this in, but
i'd say in general that they're not that common. Without knowing what you're
trying to accomplish it's impossible to point you any further.
 
J

Josef Moellers

H. Wade Minter said:
Is there a preferred module, under Linux, to verify whether a given process
ID number is still running? Nothing looked promising via a CPAN search, and
I didn't get anywhere with "perldoc -q process" or "perldoc -q pid".

There were a few modules dealing with pid files, but nothing obvious if
you just had the pid number in a scalar and needed to check it.

Besides the "kill 0" mentioned, you might look at /proc, if you restrain
yourself to Linux.
If /proc/<pid> exists, you could also check /proc/<pid>/cmdline to
verify that the process is (still) running the program you expect it to
run. Note that /proc/<pid>/cmdline has NUL bytes separating command line
arguments, so you might also want to use split /\0/.

Btdt,

Josef
 
A

Anno Siegel

Abigail said:
A. Sinan Unur ([email protected]) wrote on MMMMCDX September
MCMXCIII in <URL:## ##
## > Is there a preferred module, under Linux, to verify whether a given
## > process ID number is still running? Nothing looked promising via a
## > CPAN search, and I didn't get anywhere with "perldoc -q process" or
## > "perldoc -q pid".
##
## perldoc -f kill


Well, you can find out something in some cases, but you cannot use kill
to find out whether a given process ID isn't running.

I presume your suggestion is 'kill 0, $PID'. Sure, if that returns true,
the process is running. But if it's false, it just means you can't send
it a signal. This can be because there's no process with that process ID,
but it can also be because the process sending the signal doesn't have
UID 0, and doesn't have the same UID as the process the signal is send to.

use Errno;
$alive = kill( 0, $PID) || $!{ EPERM};

does a decent job, as far as it's portable.

Anno
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top