What to do if your cron job is failing

Hi,

If you’re finding that your cron job is failing often, its most likely one of the two problems:

  1. Your server was down when WebBasedCron tried running your file OR
  2. 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


6 Comments on “What to do if your cron job is failing”

  1. webbasedcron says:

    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 says:

    …or you can add ignore_user_abort(true); in php…then when the connection is closed, the script continues to run.

  3. […] 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 says:

    you just write :

    ? >
    set_time_limit(420);
    ignore_user_abort(true);

    function() {

    }
    ? >

    and it leaves you work 420 seconds ;)

  5. Matt says:

    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. SLT-A77 says:

    La migliore informazione essenziale è questo, che si cerca di fare molto bene in avvicinamento per quel corpo senza fissa dimora e la loro mano. Sono così ispirato a tale proposito, dopo aver letto questa tua articoli utili scritti e le vostre opinioni anche che è così attraente per fare qualcosa per quei


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s