Apr 192008
 

If you use a shared-hosting web service, it may be more difficult to run a PHP script through Cron (Crontab).  If your script uses MySQL calls for example, your hosting provider may not provide access to the MySQL library when your script runs through Cron.  Here’s a method that works just as well.

Normally you might have a cron entry like this:

10,0,*,*,*,/usr/bin/php /path/to/myscript.php

But if the script contains calls to MySQL, you might get the error message:

PHP Fatal error: Call to undefined function mysql_connect()

In this case your hosting provider does not offer MySQL access when your script is executed through Cron.  You can try placing the following code as the first line in your script file:

#!/usr/bin/php -q

and then change your cron entry to:

10,0,*,*,*,/path/to/myscript.php

But you might get the error:

/usr/bin/php: bad interpreter: No such file or directory

Perhaps you can figure out the correct directory to specify for the PHP interpretter.  A simpler method is to use cURL.  This utility executes your script as if it were retrieved via an Internet browser.  Remove the

#!/usr/bin/php -q

line from your script and change your Cron entry to:

10,0,*,*,*,/usr/bin/curl -s http://www.example.com/myscript.php

You’ll probably want to do a

chmod 760

on myscript.php so the outside world cannot read or execute it.  The -s parameter tells cURL to run in silent mode.  Otherwise you’ll get an e-mail with a progress indicator every time the script runs.

 Posted by at 2:46 pm

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)

*

Optionally add an image (JPEG only)