Open In App

How to setup cron jobs in Ubuntu

The Cron software utility is a time-based job scheduler in Unix-like operating systems. Cron allows Linux and Unix users to run commands or scripts at a given time and date. Once can schedule scripts to be executed periodically. It is usually used for system admin jobs such as backups or cleaning/tmp/ directories and more.

The following steps to be followed to set up a cron job in Ubuntu:



Before we take example of cron tab execution let’s understand the common syntax of cron tab:

Syntax:

* * * * * /path/to/command arg1 arg2
OR
* * * * * /root/backup.sh

In the syntax first * stand for representing minutes [0-59]. Second * stands for representing hour[0-23]. Third * stand for representing day [0-31]. Fourth star stands for representing month[0-12]. Fifth * stand for representing day of the week[0-7].

 
After all step for installation of cron tab and understanding common syntax, let’s execute a cron tab with suitable example.

Example #1: If we want to schedule a backup on first day of each month at 9 PM, the following command performs this operation.

#crontab -e //install your cron job by running this command.
// Append the following entry.

0 9 1 * * /path/to/script/backup-script.sh

 
Example #2: Set up and run php script as cron job to run script every day at 10 AM.

#crontab -e //add cron job

// Append the following entry.
0 10 * * * /path/to/myphpscript.php

Following options are available in crontab:
crontab -l : List the all your cron jobs.
crontab -r : Delete the current cron jobs.

For more information about cron, one can check the manual pages using:

man cron
man crontab 
Article Tags :