Tjorriemorrie
Tjorriemorrie

Reputation: 17282

Cronjob timing out with internal server error

My hosting provider only allows me to run a cronjob every 2 hours. Since I need it to run every second, I've made a page that will run for 2 hours. Also, I developed with ZF; so the page needs to be bootstrapped, which means the cronjob needs to call a web address.

So far my bash script runs every two hours:

#!/bin/bash

wget "http://www.mysite.co.za/ajax/cron"

However, it stops after a few minutes with the following error:

--2011-02-04 09:32:32--  http://www.mysite.co.za/ajax/cron
Resolving www.mysite.co.za... 123.40.97.157
Connecting to www.mysite.co.za|123.40.97.157|:80... connected.
HTTP request sent, awaiting response... 500 Internal Server Error
2011-02-04 09:37:33 ERROR 500: Internal Server Error

I don't know much about cronjobs/bash scripts, but I think it's timing out? Can anyone confirm this? If so, I'll look into curl_mutl_init to send a response back and keep the process alive at the same time - I don't think I'll have access to process control functions.

If anyone has any tips/advice, please don't hesitate.

Upvotes: 0

Views: 1902

Answers (3)

ehudokai
ehudokai

Reputation: 1926

If you have a cron that needs to be run every second, It might be better to use page loads to fire off your script. So each time someone requests a new page, it would run the script. Cron isn't capable of running on intervals any less than a minute anyway.

You can also write a php script that has the following code

continuous.php:

while(true){
     sleep(1); //pause 1 second
     exec("wget http://youraddress.com");
}

bash script to see if your php script is running

checkContinuous.sh

#!/bin/sh
#check if script is running
CHECK=`ps -ef |grep continuous.php`;
if [ -z "$CHECK" ]; then
#script not running, start it
   php /path/to/continuous.php& ; #start script and fork it
fi;

Hope that helps!

Upvotes: 0

Shrinath
Shrinath

Reputation: 8118

whatever backend server side script you are using, ask it to run for infinite amount of time OR ignore user abort.

Your webserver might have some settings to increase time out period too.. (Apache has it, I've used this feature )

Upvotes: 0

David Weinraub
David Weinraub

Reputation: 14184

Several comments observations:

1. Bootstrapping

It is not the case that a ZF task using the bootstrap needs to come via an HTTP request. You can run command-line tasks that use your bootstrap.

In your my_script.php - typically placed in directory scripts or in application/scripts - you can start with much of the same content as your web app's index.php file: check/set include_path, check/set APPLICATION_ENV, check/set APPLICATION_PATH, and instantiate your Zend_Application object.

The only difference is that you call $application->bootstrap(), but don't proceed along to $application->run(). In fact, since some of the bootstrapped resources might be needed only for the web app, you can call:

$application     
    ->bootstrap('necessaryResource1')  
    ->bootstrap('necessaryResource2')
    ->bootstrap('necessaryResource3');`

so you don't bootstrap resources you don't need.

Then go on to perform the specifics of your task.

2. Command line

If you wish to design your script to accept parameters, you can use Zend_Console_GetOpt. It's pretty cool.

3. Once-per-second?

Of course, I don't know your use-case or your specific need, but running a job once per second sure sounds like a lot to me. Is it possible to put some of these tasks into a queue and consume the queue with a frequency allowable under your hosting?

Hope this helps!

Upvotes: 1

Related Questions