gallagher118 has asked for the wisdom of the Perl Monks concerning the following question:

Running in linux; if i execute something with backticks or system; is there anyway to then send a control C into that process via perl? Or does that have to happen outside of perl? I want to use that on trace-cmd; which prompts on waiting for control c. If i kill the process; that does not seem to get handled by trace-cmd in the same way as doing a control c. (it does not generate a trace.dat and my attempts to join the trace files it does make so far have not worked.) It would seem id be better off with ftrace or the daemon option. I messed with daemon and could not get it working but overall do not want to go down another rabbit hole... Just seems like there should be a perl way to get a control c into this process... If there isn't a perl way; i guess i'll suck it up.... Thanks Thanks
  • Comment on Control C; into a running system / exec?

Replies are listed 'Best First'.
Re: Control C; into a running system / exec?
by tybalt89 (Monsignor) on Jun 23, 2025 at 01:41 UTC

    Neither backticks (qx) or system return control to the perl program until they exit. You will have to do your own fork() call so the parent perl program can do something while the child is running. Here's a small example of a parent sending SIGINT (what control-c sends) to a child.

    Hope this helps...

    #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11165441 use warnings; $SIG{__WARN__} = sub { die @_ }; if( my $pid = fork ) { # parent sleep 1; kill 'INT', $pid; sleep 1; kill 'INT', $pid; sleep 1; kill 'TERM', $pid; print "parent waiting for child to exit\n"; wait; print "parent exited\n"; } elsif( defined $pid ) { # child print "child started\n"; $SIG{INT} = sub { print "child got SIGINT\n" }; $SIG{TERM} = sub { print "child got SIGTERM\n"; exit }; sleep 1 while 1; } else { die "fork failed"; }
Re: Control C; into a running system / exec?
by haukex (Archbishop) on Jun 23, 2025 at 11:14 UTC

    See my node Calling External Commands More Safely: IMHO on Linux IPC::Run is the best way to manage external commands while your Perl script does something else, like sending that process a signal (as others have pointed out, Ctrl-C is SIGINT):

    #!/usr/bin/env perl use warnings; use strict; use IPC::Run qw/start/; print "Starting trace-cmd...\n"; my @cmd = ('sudo','trace-cmd','record', '-p','function','-cP',$$); # just an example my $h = start \@cmd, \my $stdin, \my $stdout, \my $stderr; # wait for trace-cmd to say something before continuing $h->pump until length $stdout || length $stderr; #print "<<$stdout||$stderr>>\n"; # just for debugging # This sleep is just a stand-in for something more complex print "Waiting 2 seconds...\n"; sleep 2; print "Sending SIGINT...\n"; $h->signal('INT'); print "Finishing...\n"; $h->finish or die "trace-cmd record returned $?"; print "Done, resetting...\n"; system('sudo','trace-cmd','reset')==0 or die "trace-cmd reset returned $?";
Re: Control C; into a running system / exec?
by hippo (Archbishop) on Jun 23, 2025 at 09:06 UTC
    If i kill the process; that does not seem to get handled by trace-cmd in the same way as doing a control c.

    Different signals are handled differently. The default sent by kill is SIGTERM whereas ^C is usually bound to SIGINT. Try using kill -INT from the shell and you should get the same effect as ^C.

    If you use exec or system to start the subprocess then it will receive the SIGINTs sent by the controlling shell (eg. by entering ^C) and there is nothing special which you need to do in the Perl parent to achieve this behaviour.

    To demonstrate, here is inttest.pl followed by inttrap.sh. Make them both executable and then run the former. You will see that your ^C keypresses successfully send SIGINTS to the child shell script.

    #!/usr/bin/env perl use strict; use warnings; print "With system ():\n"; system "./inttrap.sh"; print "With exec ():\n"; exec "./inttrap.sh";
    #!/bin/bash trap "echo interrupted" 2 for i in {1..5} do sleep 1 echo Waiting $i ... done

    🦛

Re: Control C; into a running system / exec?
by swl (Prior) on Jun 22, 2025 at 22:09 UTC