Month: April 2016

PHP Cron Management System Zend Framework 1

When you have a large number of PHP applications running on a server and each of those apps needs it own set of cron job, managing the cron tab can become a nightmare.  Rather than manage each apps cron jobs piecemeal I prefer to turn the cron problem on it’s head and manage the cron jobs from the PHP application itself.

To start with I setup three scripts in the cron system.  For RedHat based system these scripts can be placed in “/etc/cron.daily”, “/etc/cron.hourly” and “/etc/cron.monthly”.  See “http://superuser.com/questions/402781/what-is-run-parts-in-etc-crontab-and-how-do-i-use-it“. In other system you can set them up to run directly from crontab, ideally from the same user id your web pages run under.

The bash script looks like:

The export lines setup the environment, “ZEND_FRAMEWORK” is the path to the common Zend Framework files.  “PHP_INCLUDE_PATH” should match the “php_value include_path” from your Apache config.  “APPLICATION_ENV” should match your production environment and should match the environment name from your “application/configs/applicaiton.ini” or other Zend Framework ini config files.

The “PATH” is the the full path to the directory that contains all the web apps you want to run con jobs for.

“REPEAT” is the script name you will run.  So if “REPEATE=hourly” the script inside the web app that would be run is “cron.hourly”.

When put all together this script will look through all the directories in “/var/www/production” looking for a directory called “cli” running every script file called “cron.hourly” it finds.

By putting this script in each of the “/etc/cron.hourly”, “/etc/cron.daily”, “/etc/cron.weekly” and “/etc/cron.monthly” then changing the “REPEAT” to match the directory frequency, you will have a cron system the will scan your apps at regular intervals looking for cron jobs to run.