Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Catching Kill Process

by Jabox (Sexton)
on Jun 24, 2014 at 16:06 UTC ( #1091069=perlquestion: print w/replies, xml ) Need Help??
Jabox has asked for the wisdom of the Perl Monks concerning the following question:

Greetings Monks!

I need assistance with a program to catch a kill process, finish the command, and then end script without just getting cut off halfway and not finish. I will try my best to explain

Files : Perl Program, Custom Module.

Perl Program - Asks for "command, Interval, Location"

What I wanted it to do:

Perl Program calls Functions in Custom Module. Module runs "mpstat 5 ." forever.

When using Kill command "Control+C", mpstat will stop, continue to the rest of the code in the module which Creates .html and .png graph files with what ever result mpstat has made.


What it does :

Closes while mpstat is still running, doesn't create files or graphs.

Some code examples:


my $objstat = new CustomModule; $objstat->mpstat('mpstat',$interval,$path) `mv mpstat*.html html/folder`; `mv mpstat*.png png/folder`;


my $cmd = shift; my $interval = shift; my $path = shift; my $obj = new CustomModule2; `$cmd -P ALL $interval >> $path`; ## uses the command output information and assigns them into arrays## my @pngnames = ( $obj->createGraph($path, $numsamples.. etc etc) ); $obj->writeHTML($path,$numsamples,@pngnames);

So I want the program to just get the information, calls function in module where command is ran until process is killed, then moves the files to designated location

I want to make it where it kills only the 'mpstat' commands if I use the "control+c", the code still continues and does the part where the output information are assigned to arrays and made into html and png files

What I've tried:

my $cmd = shift; my $interval = shift; my $path = shift; my $obj = new CustomModule2; `$cmd -P ALL $interval >> $path`; $SIG{INT} = \&signal_handler; $SIG{TERM} = \&signal_handler; sub signal_handler { ## uses the command output information and assigns them into arra +ys## my @pngnames = ( $obj->createGraph($path, $numsamples.. etc etc) ); $obj->writeHTML($path,$numsamples,@pngnames); }

I gave it a guess to see if it works since I don't understand it and it doesn't work...

Anyone know how to do this or know where I can read up on it? I've been searching around the web and some pages just prints "caught signal" but doesn't really tell me how to make it do what I wanted it to do

Thank you in advance and if it doesn't make sense or need more information let me know please!

Replies are listed 'Best First'.
Re: Catching Kill Process
by zentara (Archbishop) on Jun 24, 2014 at 16:24 UTC
Re: Catching Kill Process
by Anonymous Monk on Jun 25, 2014 at 05:02 UTC

    You set up the signal handler AFTER you've run the command. That's too late.

    Perhaps the following will be of use:

    { local @SIG{'INT', 'TERM'} = (sub {warn 'Interrupt!'}) x 2; `$cmd -P ALL $interval >> $path`; } # rest of the code in the module which Creates ... files
    The interrupt handler is a no-op basically. You cannot, however, set it to IGNORE. That'd also ignore the interrupt in spawned $cmd.

      { local @SIG{'INT', 'TERM'} = (sub {warn 'Interrupt!'}) x 2; `$cmd -P ALL $interval >> $path`; }

      This works the way I pictured it to, thank you! But for some reason it doesn't work for this one.

      { local @SIG{'INT', 'TERM'} = (sub {warn 'Interrupt!'}) x 2; `$cmd -P ALL $interval | >> $path`; }
      Processing mpstat-5_EDT Number of samples: 9 Processing iostat-5_EDT Number of samples: 50 Processing vmstat-5_EDT Number of samples: 0

      And VM stat is the only one that runs with

        Ah nevermind I found out... I placed that code inside and now it finishes the way I wanted it to! :)
Re: Catching Kill Process
by Anonymous Monk on Jun 25, 2014 at 04:02 UTC
    When control-C is pressed, a signal will be sent which you must trap. You then should send some kind of acknowledging message to the user, then wait for the child-process to terminate. (If you need to send signal to the child, in order to get it to stop, you must send that signal yourself.) Be sure to respond only to the first occurrence of the signal, then, I suggest, remove the signal-handler. If the user presses control-C a second time, the result should probably be that everything dies-off as it is doing now.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1091069]
Approved by Old_Gray_Bear
Front-paged by toolic
[choroba]: Dangers of WFH: a mailman rings, brings a parcel for my wife, I need to go outside, take my son with me, sign a paper *and* into the postman's mobile app, get back. Guess what the soup was doing meanwhiles?
[Discipulus]: soup without heat protections as CPUs are doomed to burned..
[1nickt]: The dangers of milk-based soup, you mean?
[Corion]: choroba: Ouch! But you don't need that many external events for that. I have on several occasions set up milk to cook and then programmed a bit, only to find the milk burned and congealed over the stove :-/

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2017-10-18 11:37 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (244 votes). Check out past polls.