Determine a child PID

Discussion in 'Perl Misc' started by Joey, Jul 15, 2003.

  1. Joey

    Joey Guest

    I am trying to determine the PID when opening of a file handle from within
    my main perl code. Here is a snip where i call the open file handle.

    $infile = 'infile';
    open(IF, "tail -f $infile|") or die "Cannot open pipe $infile\n";

    while (<IF>) {
    do_smothing()
    }

    In the example below in unix the main perl code has a pid of perl variable
    $$ (PID=18629) then the open file handle has another PID, a child of the
    parent (PID = 18630)

    % ps -ef| grep tail
    user 18632 18455 0 10:42:06 pts/10 0:00 grep tail
    user 18630 18629 0 10:42:02 pts/10 0:00 tail -f infile
    % ps -ef| grep myscript.pl
    user 18634 18455 0 10:42:12 pts/10 0:00 grep myscript.pl
    user 18629 18455 0 10:42:02 pts/10 0:00 /usr/bin/perl ./myscript.pl

    Is there a way within perl to get the child PID 18630 ?

    Thanks in advance.
    Joey
    Joey, Jul 15, 2003
    #1
    1. Advertising

  2. Joey

    John Strauss Guest

    On 15 Jul 2003 07:47:14 -0700
    (Joey) wrote:
    >
    > I am trying to determine the PID when opening of a file handle from within
    > my main perl code. Here is a snip where i call the open file handle.
    >
    > $infile = 'infile';
    > open(IF, "tail -f $infile|") or die "Cannot open pipe $infile\n";
    >
    > while (<IF>) {
    > do_smothing()
    > }
    >
    > In the example below in unix the main perl code has a pid of perl variable
    > $$ (PID=18629) then the open file handle has another PID, a child of the
    > parent (PID = 18630)
    >
    > % ps -ef| grep tail
    > user 18632 18455 0 10:42:06 pts/10 0:00 grep tail
    > user 18630 18629 0 10:42:02 pts/10 0:00 tail -f infile
    > % ps -ef| grep myscript.pl
    > user 18634 18455 0 10:42:12 pts/10 0:00 grep myscript.pl
    > user 18629 18455 0 10:42:02 pts/10 0:00 /usr/bin/perl ./myscript.pl
    >
    > Is there a way within perl to get the child PID 18630 ?
    >
    > Thanks in advance.
    > Joey


    open() should do that for you. it's in "perldoc -f open",
    the bit where it says: "If the `open' involved a pipe, the
    return value happens to be the pid of the subprocess."

    which is nice.




    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    drop the .thetenant to get me via mail
    John Strauss, Jul 15, 2003
    #2
    1. Advertising

  3. [posted & mailed]

    On 15 Jul 2003, Joey wrote:

    >I am trying to determine the PID when opening of a file handle from within
    >my main perl code. Here is a snip where i call the open file handle.
    >
    >$infile = 'infile';
    >open(IF, "tail -f $infile|") or die "Cannot open pipe $infile\n";


    When open() is doing more than just opening a file (that is, when you're
    piping), it returns not only success or failure, but the actual PID.

    my $pid = open(TAIL, "tail -f $infile |")
    or die "cannot `tail -f $infile`: $!";

    --
    Jeff Pinyan RPI Acacia Brother #734 2003 Rush Chairman
    "And I vos head of Gestapo for ten | Michael Palin (as Heinrich Bimmler)
    years. Ah! Five years! Nein! No! | in: The North Minehead Bye-Election
    Oh. Was NOT head of Gestapo AT ALL!" | (Monty Python's Flying Circus)
    Jeff 'japhy' Pinyan, Jul 15, 2003
    #3
    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. Replies:
    3
    Views:
    6,697
    Stefan Schulz
    Jul 24, 2005
  2. Jeff Rodriguez
    Replies:
    23
    Views:
    1,119
    David Schwartz
    Dec 9, 2003
  3. Aless

    child pid

    Aless, Apr 25, 2004, in forum: C Programming
    Replies:
    3
    Views:
    511
    August Derleth
    Apr 26, 2004
  4. David Hirschfield
    Replies:
    3
    Views:
    3,288
    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:
    450
    MrJean1
    Feb 4, 2006
Loading...

Share This Page