Perl: die if child process is interrupted

27 Jan 2011

You are using system to run a child process, and you would like your program to terminate if the process happens to be interrupted, e.g. by pressing ⌃ ctrlC.

die "Child process terminated\n" if (system($cmd) & 127) == 2;

In general system($cmd) & 127 is the signal number, if any, that caused the child process to terminate.

Note that, the wait status (returned by system) of the process you want might be right-shifted by 8 bits if it wasn’t the child, but the child of the child, the one that got the interruption.