Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re^3: perl alarms not working as expected

by betterworld (Deacon)
on Oct 16, 2012 at 17:20 UTC ( #999385=note: print w/ replies, xml ) Need Help??


in reply to Re^2: perl alarms not working as expected
in thread perl alarms not working as expected

The signal handler is that sub{} that you stored in $SIG{ALRM}.

To kill the other script, you'd have to get its pid. Unfortunately I don't know any way to this without any ugly hacks. Maybe someone else does.

So for instance, you can use this temporary file handle hack:

#!/usr/bin/perl use strict; use warnings; use CGI; use File::Temp; my $q = new CGI; print $q->header; my $tmp = File::Temp->new(UNLINK => 0); my $tmpnam = $tmp->filename; eval { local %SIG; $SIG{ALRM}= sub{ $tmp->close; system("fuser", "-sk", "-TERM", $tmpnam); unlink $tmpnam; die "timeout reached, after 20 seconds!\n"; }; alarm 3; #sleep (60); system("sleep 10 3>$tmpnam"); alarm 0; }; alarm 0; if($@) { print "Error: $@\n"; } exit(0);

This requires the "fuser" command, which is shipped with most Linux distros.

If this does not make the other script die, then leave out the "-TERM".

I replaced the name of your shell script with "sleep 10", otherwise I can't test it. I also replaced 20 with 3 because I did not want to wait 20 seconds.


Comment on Re^3: perl alarms not working as expected
Select or Download Code
Re^4: perl alarms not working as expected
by kavkazi (Initiate) on Oct 16, 2012 at 19:18 UTC

    Thank You @betterworld for the line: system("sleep 10 3>$tmpnam"); what does 3>$tmpnam do? If I need to replace the sleep 10 with a call to script that needs arguments, how would I do that? i.e: system("/opt/bea/domains/fsa/scripts/start.sh fsaAs02 -q 3>$tmpnam");

      system("/opt/bea/domains/fsa/scripts/start.sh fsaAs02 -q 3>$tmpnam");

      Yes, that seems right.

      Shell redirections:

      "command > file" redirects standard output into a file. Standard output is file descriptor 1.

      "command < file" redirects standard input from a file. Standard input is file descriptor 0.

      Standard error is file descriptor 2, and the first free descriptor is usually 3. Therefore I used "command 3> file" so that the (dummy) file descriptor 3 is connected to a temporary file, which was created using File::Temp.

      In my experience, open files are a very nice means to keep track of a running process. The command "fuser" can be used to find the processes that are connected to a file. "fuser" will kill these processes when "-k" is specified.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://999385]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2014-12-21 03:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (102 votes), past polls