What to do if your cron job is failing
December 7, 2005
Hi,
If you’re finding that your cron job is failing often, its most likely one of the two problems:
- Your server was down when WebBasedCron tried running your file OR
- Your script took longer than the max execution time allowed. (most likely)
When a cron job is created, its given a maximum of 30 seconds execution time. You can open a support ticket, and we can increase your max execution time.
Some of our customers have cron jobs which take a long time to execute. For example, a blog related script could take up to 5 minutes (or more) to finish. WebBasedCron offers cron job execution by the minute, so allowing a cron job to take 5 minutes to run is dangerous.
I’m going to explain a solution below, were your cron job can take as long as you want, and WebBasedCron will never stop it. The solution below uses PHP coding, but a similair type of solution can be created with any server side language (i.e. ASP, etc.)
Lets say the script you want to run is http://www.yourdomain.com/post.php
Normally, you would go to WebBasedCron and set a cron job to run the URL above.
Now, instead of running that URL directly, we’re going to make a new file call it wbc.php – so the URL for this file would be http://www.yourdomain.com/wbc.php.
Inside wbc.php you would have the following:
$command = “php -d max_execution_time=’10000′ -f post.php >/dev/null &”;
exec($command);
Basically, the wbc.php script, when called by WebBasedCron, will run post.php in the background on your server. This way, WebBasedCron will run wbc.php and leave your server. But in actual fact post.php will have been running in the background on your server, and will continue to execute till it finishes.
The:
max_execution_time=’10000′
means that post.php will be executed for a maximum of 10,000 seconds (change this as you like). The part which makes post.php run in the background is the ‘&’ in command.
To download the file click here. Just remove the .txt.
If you have any questions, please post a comment or open a support ticket.
Thanks,
George
WebBasedCron
Entry Filed under: WebBasedCron. .
6 Comments Add your own
Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
Trackback this post | Subscribe to the comments via RSS Feed
1.
webbasedcron | December 8, 2005 at 4:36 am
If your using a windows machine, the exec command might be a bit different. I’ll look into this and post a solution
George
2.
FinalArena | March 18, 2006 at 11:15 am
…or you can add ignore_user_abort(true); in php…then when the connection is closed, the script continues to run.
3. WebBasedCron blog & faq » A great way to never have your PHP script fail | April 8, 2006 at 6:53 pm
[...] A little while ago I wrote a post about how to prevent your cron job from failing. A comment was posted suggesting the use of the PHP function ignore_user_abort(true). This is a great idea; anyone who is running a PHP script should put this: [...]
4.
totodu web | March 12, 2007 at 2:46 pm
you just write :
? >
set_time_limit(420);
ignore_user_abort(true);
function() {
…
}
? >
and it leaves you work 420 seconds ;)
5.
Matt | March 11, 2008 at 2:45 pm
Hi,
That’s great, but I don’t see how it would work for me. My website is running ASP.NET 2.0 on a shared hosting environment and I don’t think I would have permission to run jobs in the background.
The job I’m looking to automate is a mass email up to 1000 recpients. I have negotiated to split the task into around 10 batches set off at 60 second intervals, so my script takes 10 minutes total to run (this limitation suggested by hosting company to avoid jamming the mail server). I could set up 10 cron jobs I suppose, but is there a better solution using your system?
Thanks.
6.
Php Catalog | December 22, 2009 at 3:23 am
it’s easy to increase the max execution time