Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Giving status of a file..

by Rhodium (Scribe)
on Apr 15, 2002 at 02:15 UTC ( #159064=perlquestion: print w/replies, xml ) Need Help??

Rhodium has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,
I have a file (~/.dracktrack) it consists of lines of data which represent the following
<$pid> <$dir> <$name>
For each line, I want to do the following
  • Check to see if the pid is running if it is print Process $pid running $name in $dir \n
  • If it isn't running delete the line.
  • This seems simple enough, but I can't seem to figure out how to tell if a pid is running. I have seen references to "kill" but I don't want to do that..
    Any ideas?


    The <it>seeker</it> of perl wisdom.

    Replies are listed 'Best First'.
    Re: Giving status of a file..
    by Chmrr (Vicar) on Apr 15, 2002 at 02:25 UTC

      See the perldocs for kill. If you send the signal 0, it doesn't really kill the process, just lets you know if it was able to send the signal -- which turns into if the process id was alive to recieve it. In fact, kill is perhaps a misnomer, as it implies that it always sends a SIGKILL to the process; perhaps signal might haver been a better name for the construct.</code>

      Update: It's funny how sometimes a good textual description of the problem translates so well to pseudocode and thence to code. If you read the above description, it's a fairly good summation of how one might go about coding the problem, nearly line-by-line. Of course, one could be perverse and dop it in about five statements using a hideous map / grep / join combination, but to each their own..

      perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

    Re: Giving status of a file..
    by stephen (Priest) on Apr 15, 2002 at 02:30 UTC

      I think kill is the way to go... If you have a bunch of PIDs, and want to see if they are running, you can do:

      # untested my @proc_ids = (445, 320832, 38393, 327334); my @running = kill 0, @proc_ids;

      kill won't kill the processes here. It will just return the process ID number if it is running.

      Otherwise, you might want to check out Proc::Simple (which I suspect just uses kill behind the scenes, but don't know). Is there a particular reason you don't want to use kill?


      A reply falls below the community's threshold of quality. You may see it by logging in.
    Re: Giving status of a file..
    by thor (Priest) on Apr 16, 2002 at 03:14 UTC
      Of course, you can't be assured that pid 45872 is the same now as it was 5 minutes ago. What is to say that the process that you were trying to keep track of didn't exit, and a new one took it's place? Based on your description, you will incorrectly judge your process to still be running. Bad mojo indeed. Depending on what you're doing, you may want to take a look at fork in conjunction with flock to fork off a bunch of children that write to a common log file. They can log their own births and deaths. I recently used this trick to query off of a partitioned database, and I must say that it worked well.
    Re: Giving status of a file..
    by jreades (Friar) on Apr 15, 2002 at 18:42 UTC

      Is there any reason you wouldn't want to do this:

      my @processes = (); open (PROCESSES, "ps -fu $user|") or die ("Couldn't open process list: + $!"); while ($process = <PROCESSES>) { chomp($process); my @process = split(/\s+/, $process, 8); push @processes, \@process; } close PROCESSES;

      Just curious

    Log In?

    What's my password?
    Create A New User
    Node Status?
    node history
    Node Type: perlquestion [id://159064]
    Approved by rob_au
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (3)
    As of 2021-05-17 01:40 GMT
    Find Nodes?
      Voting Booth?
      Perl 7 will be out ...

      Results (152 votes). Check out past polls.