Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

System Commands

by azheid (Sexton)
on Jul 07, 2016 at 13:46 UTC ( [id://1167375]=perlquestion: print w/replies, xml ) Need Help??

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

I have a perl script that calls another set of perl scripts with the system() command.

What I want to know is the behavior of perl when a perl script is killed with ctrl-c. Specifically, if a system() command has already been called, but the system() command process is still running, what happens when the original script is killed?

Will the called process continue to execute, or will it die with the killed script?

Replies are listed 'Best First'.
Re: System Commands
by stevieb (Canon) on Jul 07, 2016 at 14:00 UTC

    What happens when you try it? It's pretty trivial to test...

    Calling script that runs another script with system() (call.pl):

    use warnings; use strict; system 'perl', 'call.pl';

    Script that gets called by the one above that we'll ^C (called.pl):

    use warnings; use strict; my $c = 1; while (1){ print $c++ ."\n"; sleep 1; }

    Run call.pl to exec called.pl:

    $ perl call.pl 1 2 3 4 5

    In another terminal window, see if called.pl is running:

    $ ps ax | grep called 29221 pts/9 S+ 0:00 perl called.pl

    Yep, it is. Back to the other terminal:

    6 7 ^C

    Check if called.pl is still running after ^C:

    $ ps ax | grep called

    Nope!

    The whole process chain is killed when you kill the top process, unless you explicitly set the process called by system to go into the background, whether it be a daemon, service etc.

Re: System Commands
by choroba (Cardinal) on Jul 07, 2016 at 14:03 UTC
    Without knowing more about the details, we can't really tell. For example, pressing C-c when running the first script terminates only the inner Perl loop
    perl -wE 'say 1; system $^X, "-wE", q(sleep 1, say "\t$_" for 1 .. 10) +; say 2;'

    but the second script terminates immediately, while the system command still runs in the background and can't be stopped by C-c :

    perl -wE 'say 1; system q(perl -wE "sleep 1, say qq(\\t\$_) for 1 .. 1 +0" &); say 2;'

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: System Commands
by perlfan (Vicar) on Jul 07, 2016 at 16:07 UTC
    It depends. If your system command involves a nohup is daemonized somehow, your forked sub-process will continue.

    Do yourself a favor and convert the Perl scripts you're calling via system to Modulinos.

    You will retain the functionality of your original scripts, but they will become a callable library that requires no forking.

Re: System Commands
by Anonymous Monk on Jul 07, 2016 at 20:23 UTC

    I don't know if there's a good overview of unix process control somewhere, but for starters, take a look at NOTES section in setpgid. The gory details people rarely delve into.

    That said, normally your script and its descendants (or else a piped job maybe) will all run in the same process group. They are all interrupted or suspended together.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1167375]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2024-04-19 11:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found