Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: script suicide?

by Abigail-II (Bishop)
on Jul 21, 2003 at 07:43 UTC ( [id://276169]=note: print w/replies, xml ) Need Help??


in reply to script suicide?

To have a script kill itself after 30 seconds:
$SIG {ALRM} = sub {exit}; alarm (30);

There's however one caveat: if you use Perl 5.8.0, and your script is "hanging" during a Perl operation, the signal will be queued until that Perl operation is finished.

Abigail

Replies are listed 'Best First'.
Re: Re: script suicide?
by liz (Monsignor) on Jul 21, 2003 at 08:03 UTC
    But then you can get the "old" dangerous pre 5.8.0 signal handling back by using POSIX::sigaction. YMMV.

    Liz

Re: Re: script suicide?
by AssFace (Pilgrim) on Jul 21, 2003 at 13:58 UTC
    Hmm, trying this in a small test script didn't seem to work. Which means I'm probably not doing it right.
    The code below would make me think that it will print that out for 5 seconds and then stop via the exit call.
    Instead it just runs and runs, happy as can be.
    $SIG {ALRM} = sub {exit}; alarm (5); while(1){ print "hairy fishnuts\n"; }
    "perl -v" tells me "This is perl, v5.6.1 built for i386-freebsd"

    What am I doing wrong?

    -------------------------------------------------------------------
    There are some odd things afoot now, in the Villa Straylight.
      You either have a bug in your particular installation of Perl, or in the OS (I tried it with the same version of Perl, but different OS), or you aren't patient enough. Did you by any chance run this without redirecting standard output? Then it might take more seconds, and if you do this remotely, and have a slow network, it even takes more time. For me, the program gets aborted after 5 seconds if I redirect output - but it takes 8 seconds before getting a prompt if I don't redirect output. With a slow network, or a slow terminal, it might take minutes.

      Abigail

        Ahh, ye' ol' slow network. That would be the key here. I set it to alarm(1) and then just let it run and that worked just dandily over time.
        I am indeed on a very slow network here (128K frame relay shared between 16 people and 3 servers - blech).

        Thank you very much for the help. I'm gonna go flagellate myself now like a proper monk - that'll teach me for not heeding slow networks.
        Thanks again :)

        -------------------------------------------------------------------
        There are some odd things afoot now, in the Villa Straylight.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (3)
As of 2024-04-20 04:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found