How To Use Cron to Schedule a Task In Linux/Unix?


Gaming Laptop Series

Cron is a program that is used to automate a task in a Unix based operating system. It is basically a smart alarm clock for the system. When the alarm sounds the operating system runs the commands or script mentioned in the cron table.

You can set this alarm clock to run at all sorts of regular time intervals. Linux installs cron daemon(crond) by default. Cron is most suitable for repetitive tasks while scheduling one time tasks can be accomplished by at utitlity.

These tools are useful especially for system administrators. Ahead in this article, we will learn to execute commands or script automatically by using cron.

Cron table (crontab)

The actions performed by cron are dependent on the crontab file. Crontab is a configuration file that contains the date, time and commands or the script that is to be executed. Each line of this file represents a job and each of them consists of six fields. The possible values for these fields will be as given below –

*     *           *      *      *
|     |      |      |      |      |
|     |      |      |      |      |_______Command/Script to execute
|     |      |      |      |_____________________Day of the week(0-6) Sunday=0 or 7
|     |      |      |__________________________________Month(1-12)
|     |      |______________________________________________Day of the month(1-31)
|     |____________________________________________________________Hours(0-23)
|_________________________________________________________________________Minute(0-59)

How to add a cron job?

To add a cron job in your system first open your terminal by pressing ctrl+alt+t and then type the following command –

crontab -e

If you are using this command for the first time then it will prompt you to choose a text editor. Now this will open the configuration file of the cron table where you have to provide the values for every six fields. Use sudo with crontab -e command to schedule a task with root permissions.

For example – To schedule a backup for all your account at 1 am every week use the following configuration in you crontab file –

Using *(asterisks) instead of a specific value means every possible value of that field will be used. If you are using nano text editor once values for each field get entered press ctrl+s to save and then ctrl+x to exit from the editor. It will display a message saying installing new crontab.

Entering multiple values for a field

You can mention multiple values for a field by using a comma for example –

0 1 1,10,20,28 *  *  apt-get update &&  apt-get upgrade

Now the system will run the update and upgrade at 1 am on 1,10,20,28th days of every month for any day of the week.

Options to use with the crontab command

The list of options that can be used with crontab command is given below –

-l – This option will display the current cron jobs on the standard output

-r – The option will remove the current crontab task

-e – Used to edit current crontab on an editor specified by the user

-i – This option modifies -r option to prompt the user for a response i.e. Y/N to delete the crontab

How to list all installed cron jobs?

To view, all cron jobs for current user use the following command –

crontab -l

If you want to list all the cron jobs that belong to a specific user then use the following command-

sudo crontab –u username –l

Replace the username with the actual username.

To know more about cron and crontab see their manual page. Use the following in your terminal –

man cron

Or

man crontab

Leave a Comment

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