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

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

Replies are listed 'Best First'.
Re^3: perl alarms not working as expected
by betterworld (Curate) 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/ fsaAs02 -q 3>$tmpnam");

        system("/opt/bea/domains/fsa/scripts/ 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://999327]
[Cosmic37]: I wish thee a peachy life and express gratitude for considering my pathetic questions
[erix]: record separator
[Cosmic37]: Permissions are interesting earthlings. Did nature determine who gives permission and who asks permission. Who was the first to get permission? Are you not related to them as one big earthling family?
[karlgoethebier]: Cojones! We need cojones!
[karlgoethebier]: Ouch! Permissions! We need permissions!
[BarApp]: I can not use modules. I gain temporary access and still can not use modules.

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2017-06-29 16:39 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (672 votes). Check out past polls.