Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

best way to kill a spawned process

by redss (Monk)
on Feb 05, 2013 at 16:19 UTC ( #1017214=perlquestion: print w/ replies, xml ) Need Help??
redss has asked for the wisdom of the Perl Monks concerning the following question:

What is the best way to kill a system command spawned from my program? For example:
system "arecord file.wav"; # at some point in program user wants to end recording system "killall arecord";
Is there a better preferred way to do that in perl?

Comment on best way to kill a spawned process
Download Code
Re: best way to kill a spawned process
by tobyink (Abbot) on Feb 05, 2013 at 16:47 UTC
    use strict; use warnings; if (my $child = fork) { # foreground process sleep 5; kill 9, $child; } else { # background process exec("arecord", "file.wav"); }

    The fork plus exec combination allows you to cleanly launch a background process, and gives you the PID for the background process, which can later be passed to kill.

    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
      That works...thanks!
        Take his choice of signal with a large grain of salt [1]:
        No no no.  Don't use kill -9.
        
        It doesn't give the process a chance to cleanly:
        
        1) shut down socket connections
        
        2) clean up temp files
        
        3) inform its children that it is going away
        
        4) reset its terminal characteristics
        
        and so on and so on and so on.
        
        Generally, send 15, and wait a second or two, and if that doesn't
        work, send 2, and if that doesn't work, send 1.  If that doesn't,
        REMOVE THE BINARY because the program is badly behaved!
        
        Don't use kill -9.  Don't bring out the combine harvester just to tidy
        up the flower pot.
        
        Just another Useless Use of Usenet,

        [1] http://www.partmaps.org/era/unix/award.html#uuk9letter

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2014-09-19 01:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (129 votes), past polls