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

For an example I used ls command there.

My actual process is that,

Server is keeping on running in one side. This server will execute another application which might be executed at min 10 minutes to get the data from the database or file. The application will create a file in common directory once the data is get from the file or database.

Server will continue to do further process. It will not wait for any child process. It just checks file is available in the common directory.

Here when the application is not written the file at that time I need to kill the process with the process id.

For this how do I get the process id of the child process?

  • Comment on Re^2: Obtain the child process id in perl

Replies are listed 'Best First'.
Re^3: Obtain the child process id in perl
by moritz (Cardinal) on May 26, 2010 at 10:21 UTC

    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.

      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)

        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;