tags:

views:

80

answers:

1

I have a Perl script with the following code.

...
$kill = 1;
$exit = 0;
`kill -9 $pid >& /dev/null`;
...
print "kill=$kill exit=$exit\n";
if ($kill) {
  exit $exit;
} else {
...

In summary, this script uses open3() to run a command. At some point, it kills the job and then the intention is that the script will exit with code 0. I inserted a print statement to show the values of variables $kill and $exit, which is shown below.

kill=1 exit=0

Since $kill is 1, I would expect the script to exit with code 0 above, since $exit is 0. However, the script exits with code 9, which is the signal sent to the child. Why is the Perl script exiting with the exit code of the child, instead of that which is given to the exit() call?

A: 

From here:

The exit() function does not always exit immediately. It calls any defined END routines first, but these END routines may not themselves abort the exit. Likewise any object destructors that need to be called are called before the real exit.

Kyra
So how can I make it exit with the desired code, while still killing the child?
balor123
Irrelevant. Because he's sending `SIGKILL` (`kill -9`), the Perl process is forcibly terminated by the kernel and not allowed to run **any** shutdown or cleanup code. `exit` isn't even called - as soon as that `SIGKILL` hits, it's all over before the Perl code knows what hit it.
Dave Sherohman