Executing PHP Script Via CRON and CURL

CRON is a crucial component of a Web Server. It is where you run your scheduled tasks such as notifications, mass mailing, backup and stuff like that. I really thought it was just easy but when I have to setup a server from ground up, it was a bit tricky.

Creating a cron job is easy. Simply issue this command:

crontab -e

This will bring you to the cron job editor where you enter the time it should execute and the script / executable file to run.

For a time saver:

@hourly /home/user/script1.sh
@daily /home/user/script2.sh

will execute the specified script hourly and daily respectively. A more advanced time/date is available on the cron manual pages. Since I only need hourly and daily jobs, so that’s it.

Inside the script, you need to specify the command line interpreter and the script itself. According to PHP Docs, executing a PHP script is as simple as:

php -f my_script.php

However, there are some settings that are not available on the command line version of PHP. One of the possible cause I think is the path resolution where the PHP script path is not resolved to the path of the PHP script, but to the path of the Bash script. With a very limited time, I immediately switches to CURL.

CURL can call URL as if human visits it. Based on that, we can setup curl to execute out script which is available via URL. So here is my bash script:

#!/bin/bash
curl http://test.com/test_ada32151asd32a1s54asd1_xxx.php

So that makes a great combo! CRON + CURL = FTW!

This entry was posted in Cron, Curl, Web Development, Zend Framework. Bookmark the permalink.

One Response to Executing PHP Script Via CRON and CURL

  1. Pingback: Enable APC on PHP CLI | Lysender's Daily Log Book

Leave a Reply

Your email address will not be published. Required fields are marked *