Setup a cron job that runs on every 15 minute using Crontab in Ubuntu 16.04

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

 

Code Dissection

  1. */15 - tell Ubuntu to run the script on EVERY 15 MINUTE
  2. * - 2nd asterisk tells OS to run on EVERY HOUR
  3. * - 3rd asterisk tells OS to run on EVERY DAY
  4. * - 4th asterisk tells OS to run on EVERY MONTH
  5. * - 5th asterisk tells OS to run on EVERY DAY OF THE WEEK
  6. /usr/bin/php - indicates that PHP is running from "/usr/bin/php" folder
  7. /your/path/to_the_folder_of/php_script/your-script-filename.php - tells the OS about the actual path of your PHP script.

 

Crontab Options:

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

 

Helpful resources

To help you learn about more variant of possible setup on crontab, I have compiled some useful resources on this topic below.

 

I hope that this article will give you a head start and guide you to setup your first cron job in your Ubuntu server.

Other articles of this category:

There is no article found in this category.