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

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.

Finishes.

What it does :

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

Some code examples:

IN PERL PROGRAM

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

IN MODULE

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!

Comment on Catching Kill Process
Select or Download Code
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 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.
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 | timestamp.pl >> $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 timestamp.pl

        Ah nevermind I found out... I placed that code inside timestamp.pl and now it finishes the way I wanted it to! :)

Log In?
Username:
Password:

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
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2014-09-21 03:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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











    Results (166 votes), past polls