Heroku | Heroku cron jobs and CakePHP code

Author - Tanuj

Use Heroku cron jobs to run CakePHP code at fixed intervals of time.
Cron is a time-based scheduler used to run commands periodically at fixed intervals. Cron means time. It is used for scheduling repetitive tasks. In order to use it in Heroku, you just need to install the Add on Heroku Scheduler in heroku app. Scheduler is used for running code on regular interval of times. It allows you to run your script in every 10 minutes, every hour and every day. It just uses the cakephp file name to run the cron job.

Here, I am using it for automatic nightly syncing of data from a file in other system, so I put it in a monthly shell which runs daily at night. You can see that in below screenshot of Heroku scheduler :

In our MonthlyShell, we created a main() method which is called when the shell is called by Heroku scheduler. AppShell is a base class which contains all common functions and logics.
We can call our action like myTestFunction() and we can put it in a try-catch block so that if it fails it should throw an error information as shown below:

<?php App::import('Controller','Abc'); class MonthlyShell extends AppShell { public function main() { $abc = new AbcController(); try { $abc>myTestFunction();
} catch (Exception $e) {
		 $z = print_r($e,1);
		 echo '
Exception while running updateFromLms : '.$z;
public function myTestFunction(){
//do what you want here to be served by cron job
Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

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

Send your message