in reply to how can a perl script determine it's own PID while running

see $$
  • Comment on Re: how can a perl script determine it's own PID while running

Replies are listed 'Best First'.
Re^2: how can a perl script determine it's own PID while running
by Anonymous Monk on Aug 14, 2006 at 19:59 UTC
    OK - this is perfect (exactly what I was looking for)
    Now.... I would like to take this a step further:
    I would like to take the newly found value from $$ and send it to another script, (the sending part is not a problem), and have that new program check on a regular basis (still linux OS) and see if the PID still exists... once the PID that the new program is monitoring finishes the new program will do something.
    The question is: How do I monitor said PID from the current script??
    Thanks Again for Your Great Wisdom!
      You can use the unfortunately named kill function with a signal value of zero to see if a process is still alive.

      my $pid = 1234; if( kill 0, $pid ) { # it's alive } else { # it's dead }
      i'd suggest making a /var/run/yourProgram.run file that containts the PID of your script, like so many other UNIX programs have. Have the file deleted when your script exits. then have your other script monitor that file.
        Though if your script gets severely trashed a la kill -9 the file would still be there. Usually you are best off combining the pid file with a kill check... or a check for the proc dir.

                        - Ant
                        - Some of my best work - (1 2 3)