Setup a cron job on every 15 minutes using Crontab in Ubuntu

WebTechRiser.com > Ubuntu > Setup a cron job on every 15 minutes using Crontab in Ubuntu

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

Code Dissection

  1. */15 – tell Ubuntu to run the script EVERY 15 minutes
  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 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

Helpful resources

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

Also read:  How to Install LEMP stack (Linux, Nginx, MySQL, PHP) with phpMyAdmin on Ubuntu 20.04

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.

Category Ubuntu

Leave Your Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.