Setting Cron job with cakephp 2.0 in Easy steps:
In some cases we need to do some process automatically every day /every hour / every week. For this
we can use the Cron job. To see more about Cron Job Click here.
This example will shows that sending email to customers every 6 hours based on some conditions:
My Project name is Sample and the Sample folder contains the following folders:
Sample— app— Controller etc— lib— vendors— plugins
Step 1: Go to the following folder
Project folder/app/Console/Command/
Step 2: Create a file named MyShell.php (You can create the file as per your own risk like SampleShell, CalShell etc)
Step 3: Paste the following code in that file.
class MyShell extends Shell {
function main()
{
App::import(‘Component’, ‘BusinessLogic’);
$businessLogic = & new BusinessLogicComponent();
$businessLogic->initialize();
$settings = $businessLogic->senReminderEmail();
// senReminderEmail Mail function is defined in my Controller/Components/BusinessLogicComponent.php File
}
function expireMail()
{
App::import(‘Component’, ‘BusinessLogic’);
$businessLogic = & new BusinessLogicComponent();
$businessLogic->initialize();
$settings = $businessLogic->senExpireEmail();
// senExpireEmail Mail function is defined in my Controller/Components/BusinessLogicComponent.php File
}
}
?>
Step 4 : Go to the following folder: Project folder/app/Vendor/Shell/
Inside that create a file name called cron.sh and paste the following code. Give 777 permission to this file.
#!/bin/bash
# lock logic for semaphore – http://mywiki.wooledge.org/BashFAQ/045
lockdir=/tmp/cron_sh.lock
echo >&2 “$(date ‘+%Y-%m-%d %H:%M:%S’)
#################################################”
if mkdir “$lockdir”
then # directory did not exist, but was created successfully
echo >&2 “successfully acquired lock: $lockdir”
##########################################################
# —————————————————————–
# Project name —>
# =========
# Project folder/lib/Cake/Console/cake – app Project folder/app cron
/var/www/Sample/lib/Cake/Console/cake – app /var/www/Sample/app cron
################################################################
rmdir “$lockdir”
else
echo >&2 “cannot acquire lock, giving up on $lockdir”
exit 0
fi
Step 5: To check that the cron functions are working or not, Go to the following folder in terminal
Project folder/app/ #example /var/www/Sample/app
and run the following Command :
/var/www/Sample/app$ Console/cake myshell
The above line will execute the main function that we written in the MyShell.php file
To call other function just Console/cake my function_name
example: Console/cake my expireMail
Check the above functions are executing without any errors. If it executes with out any error then go for next step
Step 6: Now we are going to set the crontab. Just go to the Project folder through terminal and type crontab -e
It will open the editor in that paste the following code:
#The below line will execute the function every 6 hours
# *
*/6 * * * /var/www/Sample/app/Console/cakeshell my -cli
/usr/bin -console /var/www/Sample/lib/Cake/Console -app /var/www/Sample/app
#The below line will execute the function named “expireMail” from
myshell.php every 6 hours and it will log the execution in to the
Home/error_file.log file
# *
*/6 * * * /var/www/Sample/app/Console/cakeshell my -cli /usr/bin
-console /var/www/Sample/lib/Cake/Console -app /var/www/Sample/app >> ~/error_file.log
No comments:
Post a Comment