I have a Perl script that starts some programs using the system()
function, in sequential order. While the script is running and a program run is ongoing, I type CTRL + C. I expect the program to terminate (which happens), but I would also expect the script to terminate (which doesn't happen).
I've built a minimum working example. Here's the Perl script:
#!/bin/env perl
sub run_the_test {
local($name, $_) = @_;
print "Starting $name\n";
return system("program.bash $name");
}
&run_the_test("test1");
&run_the_test("test2");
&run_the_test("test3");
and here's the external "program":
#!/bin/env bash
echo "Started $1"
sleep 3
echo "Finished $1"
I started the script and issued a CTRL + Z. I then did a ps -o pid,pgrp,cmd
to list the process info. I can see that the script, the bash shell it invokes and the sleep command all belong to the same process group. While the sleep
is executing I would also expect that to be the foreground process group. I've read up a bit on Linux, signals and the terminal and found that CTRL + C should send an interrupt to the entire foreground process group, thus terminating the script as well.
I've also tried using exec()
instead of system()
. This way the script does stop when triggering CTRL + C, but it doesn't run past test1.
Can anyone shed some light on what's going on here?