ViniHost Logo

How to Run PHP Scripts from Cron Jobs

Running PHP Scripts from Cron Jobs

A common method for running PHP scripts from a cron job is to use a command-line program such as curl or wget. For example, the cron job runs a command similar to the following:

curl http://example.com/script.php

This command retrieves the web page, which then runs the PHP script.

However, a better way to run PHP scripts on your website from cron jobs is to use the PHP command-line interpreter. This method is just as effective and usually faster. The following command shows how to run a script using the PHP command-line interpreter:

/usr/local/bin/php -q Ksh {HOME}/public_html/script.php

The -q option enables quiet mode, preventing HTTP headers from being displayed.

If the script uses relative paths to include files, it must be called from the correct directory:

cd Ksh {HOME}/public_html/; /usr/local/bin/php -q script.php

If your script requires special configuration options, you can use a custom php.ini file:

/usr/local/bin/php -c Ksh {HOME}/php.ini Ksh {HOME}/public_html/script.php

More Information

For more details on setting up cron jobs and running PHP scripts, refer to the documentation provided by your hosting provider.

Get PHP Hosting

Experience the ViniHost difference today and get a pre-secured, pre-optimized website. Check out our web hosting plans today.