in reply to Re^4: Obtain the child process id in perl
in thread Obtain the child process id in perl
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)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^6: Obtain the child process id in perl
by Anonymous Monk on May 26, 2010 at 12:53 UTC | |
by almut (Canon) on May 26, 2010 at 14:37 UTC |