in reply to Re^2: Obtain the child process id in perl
in thread Obtain the child process id in perl

You fork, and store the child PID in the parent. In the child you exec the application you want to run.

Or you use IPC::Run, which abstracts such things away.

See also: perlipc

Perl 6 - links to (nearly) everything that is Perl 6.
  • Comment on Re^3: Obtain the child process id in perl

Replies are listed 'Best First'.
Re^4: Obtain the child process id in perl
by Anonymous Monk on May 26, 2010 at 10:36 UTC

    I have used the following code

    my $pid = fork; unless($pid){ exec('script 2>output.log'); } print "Child Process Id:$pid\n";

    In the above example got the process id of the child process but not the process which is executed the command

    How can I take the process id of the process which is running the actual application?

      In general, you can't, unless you somehow have the actual application communicate its PID.  Problem is that the child process may fork any number of subprocesses, so the immediate child is not necessarily the application you're interested in.

      But if the idea is to kill a hanging application, you could maybe create a new process group, and then kill that entire process group.

      Update:

      #!/usr/bin/perl if (my $pid = fork()) { # kill process group after timeout sleep 3; show_procs(); kill -9, $pid; # negative signal number kills group wait; print "killed children\n"; show_procs(); # show remaining processes # ... } elsif (defined $pid) { setpgrp; # create new process group # run something (consisting of several processes) that takes a whi +le exec 'find / | grep foo'; } else { die "couldn't fork"; } sub show_procs { system "ps Tf -o pid,ppid,pgrp,sid,cmd"; } __END__ $ ./841709.pl PID PPID PGRP SID CMD 12046 12044 12046 12046 bash -rcfile .bashrc 3881 12046 3881 12046 \_ /usr/bin/perl ./841709.pl 3882 3881 3882 12046 \_ sh -c find / | grep foo 3883 3882 3882 12046 | \_ find / 3884 3882 3882 12046 | \_ grep foo 3885 3881 3881 12046 \_ ps Tf -o pid,ppid,pgrp,sid,cmd killed children PID PPID PGRP SID CMD 12046 12044 12046 12046 bash -rcfile .bashrc 3881 12046 3881 12046 \_ /usr/bin/perl ./841709.pl 3886 3881 3881 12046 \_ ps Tf -o pid,ppid,pgrp,sid,cmd

      (3882 is the PGRP in question here)

        Consider the following example. There I used setpgrp to make the process group and process group id as forked process

        check.pl
        print "process id: $$\n"; my $pid = fork; my $FH; unless($pid){ setpgrp; exec('script'); } print "Child process id is: $pid\n"; while(1) {sleep(5)}
        script file:
        use POSIX; while(1) { sleep(1); print "I am running\n"; my $pid = fork; unless($pid) { while(1) { sleep(2); print "I am child and I'am running\n"; } } }

        In the script file once again fork the process and printing the line

        From another terminal using kill -9 <Negative number of the child process id> command kill all the process belongs to this process group

        The child process is running as a defunct process but the parent process is alive

        Refer the ps command output
        643 15141 9607 0 18:14 pts/1 00:00:00 perl check.pl
        643 15142 15141 0 18:14 pts/1 00:00:00 script <defunct>

        How could I avoid this problem

        Also I need to redirect the script command output and error into one file . How can I achieve it?

      You need to avoid the intermediate shell that gets invoked because of 2>.... See exec. Replacing the redirection functionality provided by the shell involves reopening STDOUT and STDERR to the appropriate files and then executing the target program directly.

      use IPC::Open3 qw( open3 ); open(local our $TO_CHLD, '<', '/dev/null') or die; open(local our $FR_CHLD, '>', 'output.log') or die; my $pid = open3('<&TO_CHLD', '>&FR_CHLD', '>&FR_CHLD', 'script'); waitpid($pid, 0) or die;