In a Ubuntu-powered VPS, there was requirement to run a PHP script on every 15 minute. So, I fire up Google, search in Ubuntu-related resources and setup it a cron job that run on every 15 minutes. This article is a ready-reference for me and, of course, for the rest of the world who runs into similar scenario.
Cron jobs are executed by www-data user.
I used Crontab and below is the command that I issue to start new cron job:
crontab -u www-data -e
My VPS server was new and there was nothing in the list of jobs. Empty file. So, I wrote the following line of code to start a new cron job.
*/15 * * * * /usr/bin/php /your/path/to_the_folder_of/php_script/your-script-filename.php
- */15 – tell Ubuntu to run the script on EVERY 15 MINUTE
- * – 2nd asterisk tells OS to run on EVERY HOUR
- * – 3rd asterisk tells OS to run on EVERY DAY
- * – 4th asterisk tells OS to run on EVERY MONTH
- * – 5th asterisk tells OS to run on EVERY DAY OF THE WEEK
- /usr/bin/php – indicates that PHP is running from “/usr/bin/php” folder
- /your/path/to_the_folder_of/php_script/your-script-filename.php – tells the OS about the actual path of your PHP script.
To view www-data user’s current jobs, use -l (small L) switch:
crontab -u www-data -l
To delete www-data user’s current jobs, use -r switch:
crontab -u www-data -r
To help you learn about more variant of possible setup on crontab, I have compiled some useful resources on this topic below.
- Linux Crontab: 15 Awesome Cron Job Examples
- Running PHP with CRON on Ubuntu
- Official documentation
- How To Use Cron To Automate Tasks On a VPS
I hope that this article will give you a head start and guide you to setup your first cron job in your Ubuntu server.