Checking to see if PID is running

Discussion in 'Perl Misc' started by H. Wade Minter, Sep 27, 2005.

  1. 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
    H. Wade Minter, Sep 27, 2005
    #1
    1. Advertising

  2. "H. Wade Minter" <> wrote in
    news:IE1_e.71957$:

    > 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

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Sep 27, 2005
    #2
    1. Advertising

  3. On 2005-09-27, H. Wade Minter <> wrote:
    > 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.

    --
    Nathan Wagner
    Nathan Wagner, Sep 27, 2005
    #3
  4. H. Wade Minter wrote:
    > 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
    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
    Josef Moellers, Sep 27, 2005
    #4
  5. H. Wade Minter

    Anno Siegel Guest

    Abigail <> wrote in comp.lang.perl.misc:
    > A. Sinan Unur () wrote on MMMMCDX September
    > MCMXCIII in <URL:news:Xns96DDE20AB4A90asu1cornelledu@127.0.0.1>:
    > ## "H. Wade Minter" <> wrote in
    > ## news:IE1_e.71957$:
    > ##
    > ## > 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
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Sep 27, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Wil
    Replies:
    6
    Views:
    10,771
    Steve W. Jackson
    Nov 19, 2003
  2. Wil

    java checking pid

    Wil, Nov 18, 2003, in forum: Java
    Replies:
    2
    Views:
    376
  3. Guy
    Replies:
    0
    Views:
    377
  4. David Hirschfield
    Replies:
    3
    Views:
    3,276
    Thomas Guettler
    Feb 3, 2006
  5. Lars =?iso-8859-15?Q?Gust=E4bel?=

    Re: Best way to determine if a certain PID is still running

    Lars =?iso-8859-15?Q?Gust=E4bel?=, Feb 3, 2006, in forum: Python
    Replies:
    2
    Views:
    443
    MrJean1
    Feb 4, 2006
Loading...

Share This Page