Monday, February 29, 2016

How to set up a php script to run using cron in Ubuntu

For one of the customer, I wanted to execute a PHP script for every 30 minutes automatically in Ubuntu OS. This can be achieved through setting up the cron job in the ubuntu. Same thing can be achieved in Windows by using the scheduler. This was explained in previous blog "Cron Job Creation for windows server to run PHP Script".

Here we are discussing how to set up the script to run using cron job in Ubuntu. 

You can use crontab to add/remove/edit cronjobs.
Hit Alt+Ctrl+T to open terminal.
First make sure the script is executable by running:

chmod +x script.php
Then run the following command to add your cronjob:

crontab -e

Add your cronjob like this:

0,30 * * * *  pathTophp pathToScript

eg:

0,30 * * * * /usr/local/bin/php /home/username/public_html/export.php
That's it!
Your can check the current user's crontab entries by running:

crontab -l

For more information about crontab run:

crontab --help

OR

man crontab

That's it. Happy Coding!!!

No comments: