in reply to Re^3: Obtain the child process id in perl
in thread Obtain the child process id in perl
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?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Obtain the child process id in perl
by almut (Canon) on May 26, 2010 at 10:53 UTC | |
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:
(3882 is the PGRP in question here) | [reply] [d/l] |
by Anonymous Monk on May 26, 2010 at 12:53 UTC | |
Consider the following example. There I used setpgrp to make the process group and process group id as forked process check.plscript file:
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 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? | [reply] [d/l] [select] |
by almut (Canon) on May 26, 2010 at 14:37 UTC | |
Defunct ("zombie") processes arise when the respective process has terminated, but its (still alive) parent hasn't reaped the child's status by waiting for it. When you modify check.pl as follows
you'll get something like
As you can see in the next-to-last ps output, all children have been killed, but the script process is defunct, because its parent hasn't yet gotten around to reaping it (due to how I deliberately wrote the loop). Three secs later, in the last ps output, the process is no longer there, because waitpid has been called in the meantime. | [reply] [d/l] [select] |
|
Re^5: Obtain the child process id in perl
by Corion (Patriarch) on May 26, 2010 at 10:41 UTC | |
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. | [reply] [d/l] |
|
Re^5: Obtain the child process id in perl
by ikegami (Patriarch) on May 26, 2010 at 14:06 UTC | |
| [reply] [d/l] |