natty_dread has asked for the wisdom of the Perl Monks concerning the following question:

Is there a way to put a timer in an script? I want to run a command which has a default timeout of 5 mins. However, after ~ 2 minutes, I know the command is gonna fail. So I want to be able to go to the next server in the list.

I haven't been able to find any info for this in perl.....

No code....no clue

20050511 Edit by castaway: Changed title from 'Timer'

Replies are listed 'Best First'.
Re: Putting a timeout into a script
by gellyfish (Monsignor) on May 10, 2005 at 13:49 UTC
Re: Putting a timeout into a script
by fireartist (Chaplain) on May 10, 2005 at 14:06 UTC
    or alarm (as it's been months since I've seen the perldoc.com site respond).
Re: Putting a timeout into a script
by zentara (Cardinal) on May 10, 2005 at 17:29 UTC
    If you want your script to have a timer, running independently of your other code, you need to look into event-based systems. Like POE, Tk, Gtk2 , WxWidgets, etc. These have an event loop, under whose control, you can time-share code blocks.

    I'm not really a human, but I play one on earth. flash japh
Re: Putting a timeout into a script
by eyepopslikeamosquito (Archbishop) on May 10, 2005 at 21:35 UTC

    The general problem of timing out a slow event is discussed in the Perl Cookbook 2nd edition, Recipe 16.21 "Timing Out an Operation" and also in perlfaq8 "How do I timeout a slow event". On Unix, I suggest using $SIG{ALRM}/alarm (carefully). On Win2K and above, I recommend the Win32::Job module.

    See also Timing and timing out Unix commands and Timing Windows commands.

Re: Putting a timeout into a script
by j_c (Novice) on May 10, 2005 at 14:18 UTC
    would sleep 360; do the job ?
      According to the OP's description of the problem: no, it wouldn't. For this would simply make the program sleep for that amount of seconds doing nothing else in the meantime - alarm as already pointed out is the way to go.

      Of course I imagine that one could make that work too, e.g. by forking a copy of the program that would do the sleep and then communicate the timeout to the parent with some form of IPC. But that, if possible, would just be an exercise in byzantine programming...