Gossamer Forum
Home : General : Perl Programming :

Kill a script if it runs for longer than a certain time?

Quote Reply
Kill a script if it runs for longer than a certain time?
Is it possible to add some code into a script where it will die/kill the process if it runs for longer than a set time (i.e 5000 seconds) ? I've been looking around, and all that people keep doing is suggesting the use of NICE Unsure

TIA

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Kill a script if it runs for longer than a certain time? In reply to
You can use Benchmark function within your script and check for the start and end times, and if the elapsed time exceeds a certain time limit, then use the die(); function.

I'd recommend using the Perl Module: Benchmark for doing this.
========================================
Buh Bye!

Cheers,
Me

Last edited by:

Stealth: Sep 17, 2003, 11:17 AM
Quote Reply
Re: [Andy] Kill a script if it runs for longer than a certain time? In reply to
Set a signal handler to trap an alarm signal, and then schedule every X number seconds. For example:

eval {
local $SIG{ALRM} = sub { die "alarm" };
alarm 10;
flock(FH, 2);
alarm 0;
};
if ($@ and $@ !~ /alarm/) { die }

----
Cheers,

Dan
Founder and CEO

LionsGate Creative
GoodPassRobot
Magelln
Quote Reply
Re: [dan] Kill a script if it runs for longer than a certain time? In reply to
Careful about using alarm() for anything complex though. You really only want to use alarm() around blocking code like flock, or something i/o. If you use alarm around a large script, there is a good chance that some other part of the code (or some other module) will also use alarm and that will break your outer alarm.

You may also want to look at BSD::Resource and using setr_limit to limit the amount of cpu time or memory the script can use.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Kill a script if it runs for longer than a certain time? In reply to
Thanks, excellent points! I don't usually use signal handling myself, but I'll definitely keep that in mind if and when I do in the future. Thanks again for the heads up =)

----
Cheers,

Dan
Founder and CEO

LionsGate Creative
GoodPassRobot
Magelln
Quote Reply
Re: [dan] Kill a script if it runs for longer than a certain time? In reply to
Hi Dan. What exactly would this do? I basically have a script that is going through a database, and scanning for certain words on all the pages it finds. Had a few complaints from the host about it running for long periods of time. What I have done, is now created a 'flag' in the database, which will be set to '1' once the link has been checked. This enables the script to be re-run, effectivly carring on from where it left off.

Would your code do this?

TIA

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Kill a script if it runs for longer than a certain time? In reply to
I don't know if this is what you are looking for, but have a look at

Proc::Background

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] Kill a script if it runs for longer than a certain time? In reply to
Hi Ivan... that looks pretty much like I would need. I'm a little confused as to how I would pass a subroutine to it though?

timeout_system($seconds, &routine);


... or something? Not sure if this is specifically for system commands, or it will work on sub routines too?

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!