Cron is a time-based job scheduler utility in Linux-based operating systems that can be used to run commands or script at a specified time, dates, or intervals.

It is very useful for system admin to perform some useful tasks automatically like, backup, clear cache, monitor disk space and many more.

You can use the crontab command to install, uninstall, create and edit the cron jobs. Each user have their own crontab files located at  /var/spool/cron/crontabs. Cron job runs in the background and continuously checks the /etc/crontab file and /etc/cron.*/ directories.

There are two types of Cron configuration files:

The System Crontab :
This crontab runs as a root user for system services.

The User Crontab :
This crontab runs as a specific user. Each user has their own crontab file. It is used to schedule user specific tasks.

In this tutorial, we will explain how to use crontab with some example.

Requirements

  • A system with Linux installed.
  • A non-root user with sudo privileges.

Different Crontab Options

Crontab has different options available to use with crontab command. All are listed below:
crontab -e :
This is used to edit or create a new crontab file.

crontab -u username -e :
This option is used to edit or create a new crontab     file for a specific user.

crontab -l :
This option is used to display the content of your crontab file.

crontab -r :
This option is used to remove your crontab file.

crontab -a filename :
This option is used to install a specific file as crontab file.

Syntax of Crontab

Basic syntax of crontab file is shown below:

* * * * * USERNAME path-of-the-command arg1 arg2

Where:
* : First * operator specifies Minute (0-59)
* : Second * operator specifies Hours (0-23)
* : Third * operator specifies Day (0-31)
* : Fourth * operator specifies Month (0-12)
* : Fifth * operator specifies Day of the week(0-7)

Basic Crontab Commands

You can edit the crontab file of root user with the following command:
crontab -e
You should see the following screen:

Choose your preferred editor and hit Enter. You should see the following screen:

You can list all cron job files with the following command:
ls /etc/cron*
You should see the following output:
/etc/crontab
/etc/cron.d:
anacron php5
/etc/cron.daily:
0anacron apport bsdmainutils cracklib-runtime google-chrome man-db passwd update-notifier-common
apache2 apt chrome-remote-desktop dpkg logrotate mlocate popularity-contest upstart
/etc/cron.hourly:
/etc/cron.monthly:
0anacron
/etc/cron.weekly:
0anacron apt-xapian-index fstrim man-db update-notifier-common

You can list all crontab jobs for the user named vyom with the following command:
crontab -u vyom -l

To delete all crontab jobs run the following command:
crontab -r

To delete all crontab jobs of the user vyom with the following command:
crontab -u vyom -r

To run ntpdate command at 8:05 AM every day and redirect the output to /dev/null run the following command:
crontab -e
Add the following line:

05 08 * * * root /usr/bin/ntpdate > /dev/null 2>&1

To run ntpdate every Monday to Friday at 10:00 AM add the following line:

00 10 * * 1,5 root /usr/bin/ntpdate > /dev/null 2>&1

To run ntpdate command daily at 8:20, 9:20 and 11:20 add the following line:

20 08,09,11 * * * root /usr/bin/ntpdate > /dev/null 2>&1

To run ntpdate command on the first and fifteenth of each month add the following line:

* * 1,15 * 1 root /usr/bin/ntpdate > /dev/null 2>&1

Use Special String in Crontab

You can also use special string instead of the first five fields in your crontab file.
All special strings are listed below:
@reboot : This will runs after system reboot.
@hourly : This will runs once an hour.
@daily : This will runs once a day.
@weekly : This will runs once a week.
@monthly : This will runs once a month.

For example to run ntpdate command after system reboot add the following line:
@reboot root /usr/bin/ntpdate > /dev/null 2>&1

To run ntpdate command every month add the following line:
@monthly root /usr/bin/ntpdate > /dev/null 2>&1

You can also take a backup of your root cron jobs with the following command;
crontab -l > /root/cron.bak

To backup a cron jobs of specific user run the following command:
crontab -u username -l > /username_cron.bak

Conclusion

That’s it for now. I hope you have now enough knowledge to add and delete any jobs using crontab in Linux. For more information, you can visit the Cron official documentation at Cron Doc.