I am trying to run Emacs as a foreground process in the terminal window using a Perl system
call. I would like to terminate both Emacs and the Perl script when I press CTRL-C
in the terminal window. However, only the Emacs command terminates. Here is an example script test.pl
:
#! /usr/bin/env perl
use strict;
use warnings;
my $cmd = shift;
for ( my $i = 1; $i <= 3; $i ++) {
my $res = system ($cmd);
if ( $res == -1 ) {
die "Failed to execute '$cmd': $!\n";
}
my $signal = ($? & 127);
my $exit_code = ($? >> 8);
print "exit_code = $exit_code, signal = $signal\n";
if ( $signal ) {
die "Command '$cmd' died with signal $signal\n";
}
}
The output of
$ test.pl 'emacs file.txt'
is (after pressing CTRL-C
three times):
^Cexit_code = 0, signal = 0
^Cexit_code = 0, signal = 0
^Cexit_code = 0, signal = 0
So Emacs is terminated, but the Perl script keeps running.. However, if I run
$ test.pl 'sleep 10'
I get output:
^Cexit_code = 0, signal = 2
Command 'sleep 10' died with signal 2
So if I run sleep 10
instead of emacs file.txt
it works fine.