In a Ubuntu-powered VPS, there was the requirement to run a PHP script every 15 minutes. So, I fire up Google, search in Ubuntu-related resources, and set up it a cron job that runs every 15 minutes. This article is a ready reference for me and, of course, for the rest of the world who run into a similar scenario.
Cron jobs are executed by the www-data user.
I used Crontab and below is the command that I issue to start a 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 EVERY 15 minutes
- * – 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 the www-data user’s current jobs, use -l (small L) switch:
crontab -u www-data -l
To delete the www-data user’s current jobs, use the -r switch:
crontab -u www-data -r
To help you learn about more variants of possible setups 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 set up your first cron job in your Ubuntu server.