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

Hey! Does anyone know how to set the execution time for a perl/cgi script so that it's more than 30 seconds (or whatever the default is)? I dont want to change it for every script... just one. any help would be greatly appreciated. Thanks! (sorry if its a dumb question, i just really need to know)

Replies are listed 'Best First'.
Re: Setting the max execution time?
by perrin (Chancellor) on Feb 20, 2003 at 20:47 UTC
    This is not actually a Perl issue. Your question is about a resource limit that your particular server implements. There is no standard for this. You have to ask the people who administer your server.
Re: Setting the max execution time?
by shemp (Deacon) on Feb 21, 2003 at 00:33 UTC
    If this is for cgi scripts, the timeouts usually occur after an interval of inactivity, not just a total running time. So, for some internal scripts at work, in the portions of the script that process for a while without any output, we send some minimal output every so many iterations, so that there is never more than a few seconds of perceived inactivity by the web server. we will generally send a period "." or some other character every so often (usually as hidden data). the code would be something like this:
    print "<DIV style="display: none;"> my $count = 0; while ( long_process_loop) { # process ... ... $count++; if ( !($count % 1000) ) { print "."; } } print "</DIV>\n";
    you may need to play with the frequency of output, and may also need to force unbuffered output ($|++), according to the situation.
Re: Setting the max execution time?
by derby (Abbot) on Feb 20, 2003 at 20:46 UTC
    alarm would be one way:

    my $OLD_SIG = $SIG{ALRM} if $SIG{ALRM}; $SIG{ALRM} = sub { die "timeout" }; eval { alarm( 30 ); # do what needs to be done alarm( 0 ); }; if( $@ ) { if( $@ =~ /^timeout$/ ) { print STDERR "A timeout occurred"; alarm(0); } else { print STDERR "Some other error occurred"; } } $SIG{ALRM} = $OLD_SIG if $OLD_SIG;

    -derby

    update: Whoa! Mis-read your question as not more than 30 seconds. I'm not sure why you would want it to run for at least 30 seconds but you could sleep.

Re: Setting the max execution time?
by zentara (Cardinal) on Feb 21, 2003 at 13:18 UTC
    Here's another simple variation:
    #!/usr/bin/perl -w use strict; my $time_to_die=0; my $timeout=5; #in seconds $SIG{ALRM} = sub { $time_to_die=1; }; alarm($timeout); while(!$time_to_die){ #tasks here print time,"\n"; } #will allow while loop to complete #after $SIG{ALRM} is received
Re: Setting the max execution time?
by Limbic~Region (Chancellor) on Feb 20, 2003 at 20:55 UTC
    How can I force my script to take at least 30 seconds to run? Here is OWTDI:

    #!/usr/bin/perl -w use strict; my $start_time = time; # All your other code goes here my $end_time = time; my $elapsed_time = $end_time - $start_time; sleep 30 - $elapsed_time unless ($elapsed_time >= 30);

    I am not exactly sure why you need your script to take at least this long to run - most people want to take less time. If this has something to do with monitoring the process or something - there is surely a better way to do it.

    Hope this helps - cheers L~R

    UPDATE: I just realized that you might want it to be allowed to run longer than 30 seconds. In that case, it is like perrin said - not a Perl issue. Someone/something else is imposing this arbitrary limit.