Consider the following example: test.pl :
#!/usr/bin/env perl use 5.10.1; use warnings; use strict; $SIG{'INT'} = sub {print "Caught Ctrl-C - Exit!\n"; exit 1;}; $| = 1;
So, if I call this program, so it uses the usual Perl loop, everything is as expected:
$ perl test.pl test.pl: doSystemLoop is:0 (use Perl loop); starting... Test value is 0 ... ... Test value is 1 ... ... Test value is 2 ... ^CCaught Ctrl-C - Exit! $
... that is, I pressed Ctrl-C, the program will exit instantly.
However, if the commands in the while are mainly composed of system calls, it becomes almost impossible to exit with Ctrl-C:
$ perl test.pl --dosysloop test.pl: doSystemLoop is:1 (use system loop); starting... Test value is 0 ... ... Test value is 1 ... ... Test value is 2 ... ^C ... Test value is 3 ... ^C ... Test value is 4 ... ^C ... Test value is 5^C ... ^C ... Test value is 6^C ... ^C ... Test value is 7^C ... ^C ... Test value is 8^C ... ^C ... Test value is 9^C ... ^C ... Test value is 10 ... ^C ... Test value is 11^C ... ^C ... Test value is 12^C ... ... Test value is 13^Z [1]+ Stopped perl test.pl --dosysloop $ killall perl $ fg perl test.pl --dosysloop Terminated $
So, in the above snippet, I hit Ctrl-C ( ^C ) like crazy, and the program completely ignores me. :/ Then I cheat by pressing Ctrl-Z ( ^Z ), which stops the process and sets it in the background; then in the resulting shell I do killall perl , and after that I execute the fg command, which will return the Perl task to the foreground - where it finally ends due to killall .
What I would like to have starts a system cycle like this, with the ability to break out / exit it with the usual Ctrl-C. Is it possible, and how to do it?
source share