How can I set the execution time of a perl skript on a webserver. Is this analog to php in the php.ini "max_execution_time" or is there another way?
+2
A:
If you're using a CGI (not mod_perl or FastCGI), it may be worthwhile just adding the following towards the top of the code:
alarm 30; # SIGALRM delivered to this process after 30 secs
If the program has no handler for SIGALRM, it will die.
Example:
use strict;
use warnings;
alarm 2;
my $n = 0;
while ( 1 ) {
print "$n\n";
sleep 1;
$n++;
}
__END__
$ perl alr
0
1
Alarm clock
You can choose what should happen when the timeout occurs, like this:
$SIG{ALRM} = sub {
# do stuff
};
You would place that code before you use the alarm().
An example there could be sending an e-mail to advise you the script took longer than expected, or whatever.
Alarms can also be used as watchdogs for specific sections of the code, like:
alarm 5; # shouldn't take longer than 5 seconds. if not, die.
do_stuff();
alarm 0; # reset the alarm
Hope this helps.
mfontani
2010-08-23 12:33:36
Thanks for this, I know what you mean.But what if I use mod_perl for apache. Where can I set the max execution time like in PHP. Does anyone knows it for example for (l)xammp?
Robert
2010-08-24 09:18:38
You can do the same thing as above, only under mod_perl it will kill the Apache child rather that just the CGI.Under mod_perl. you can use something like:PerlSetEnv PERL_RLIMIT_CPU 120. Look at mod_perl docs for that!
mfontani
2010-08-24 12:10:41