Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: perl alarms not working as expected

by kavkazi (Initiate)
on Oct 16, 2012 at 13:18 UTC ( #999327=note: print w/ replies, xml ) Need Help??


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

That's exactly what's happening. Can you please tell me how do I kill the other script from the signal handler. What is a signal handler? Please provide the code as I am a newbie :)


Comment on Re^2: perl alarms not working as expected
Re^3: perl alarms not working as expected
by betterworld (Deacon) on Oct 16, 2012 at 17:20 UTC

    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.

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (10)
As of 2014-10-21 10:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (100 votes), past polls