I am using Linux
and .sh
is in tcsh
.
I have made a very basic fork and exec, but I need help in implementing safeties to it.
Basically my perl script calls a .sh
script in a child process. But when I do Ctrl+c
to kill the parent, the signal gets ignored by the child.
1) How do I capture the SIGINT
for the child process as well?
2) The child process that runs the .sh script still STDOUT to the screen of the xterm. How can I remove this? I was thinking of doing running the script in the background
exec("shell.sh args &");
But haven't tested as I need to figure out how to keep the child from going wild first.
3) The parent process(perl script) doesn't wait on the child(.sh script). So I've read a lot about the child becoming a zombie??? Will it happen after the script is done? And how would I stop it?
$pid = fork();
if($pid < 0){
print "Failed to fork process... Exiting";
exit(-1);
}
elsif ($pid ==0) {
#child process
exec("shell.sh args");
exit(1);
}
else { #execute rest of parent}
Use waitpid in the parent thread: http://perldoc.perl.org/functions/waitpid.html
You can also redirect stdout of your exec to /dev/null:
The signal is sent to two both the parent and the child.
If that child ignores it, it's because it started a new session or because it explicitly ignores it.
Do the following in the child before calling
exec
:Actually, I would use
open3
to get some error checking.Children are automatically reaped when the parent exits, or if they exit after the parent exits.
If the parent exits before the children do, there's no problem.
If the parent exits shortly after the children do, there's no problem.
If the parent exits a long time after the children do, you'll want to reap them. You could do that using
wait
orwaitpid
(possibly from aSIGCHLD
handler), or you could cause them to be automatically reaped using$SIG{CHLD} = 'IGNORE';
. See perlipc.