1 ffd9a51f 2023-08-04 jrmu #!/usr/bin/perl
3 ffd9a51f 2023-08-04 jrmu # (Unix only) Write an infinite loop program that catches signals
4 ffd9a51f 2023-08-04 jrmu # and reports which signal it caught and how many times it has seen
5 ffd9a51f 2023-08-04 jrmu # that signal before. Exit if you catch the INT signal. If you can
6 ffd9a51f 2023-08-04 jrmu # use the command-line kill, you can send signals like so:
8 ffd9a51f 2023-08-04 jrmu # $ kill -USR1 12345
10 ffd9a51f 2023-08-04 jrmu # If you can't use the command-line kill, write another program to
11 ffd9a51f 2023-08-04 jrmu # send signals to it. You might be able to get away with a Perl
12 ffd9a51f 2023-08-04 jrmu # one-liner:
14 ffd9a51f 2023-08-04 jrmu # $ perl -e 'kill HUP = 12345'>
17 ffd9a51f 2023-08-04 jrmu use warnings;
21 ffd9a51f 2023-08-04 jrmu foreach (qw (int hup usr1 usr2)) {
22 ffd9a51f 2023-08-04 jrmu my $sig = uc $_;
23 ffd9a51f 2023-08-04 jrmu $SIG{$sig} = sub {
24 ffd9a51f 2023-08-04 jrmu my $signal = $sig;
26 ffd9a51f 2023-08-04 jrmu printf("%s: %d\n", $signal, ++$n);
27 ffd9a51f 2023-08-04 jrmu if ($signal eq 'INT') {