How to Schedule Cron Jobs Using Crontab in Linux
Cron is a very useful tool that allows you to schedule tasks on various distributions of Linux. The tasks are most commonly known as cron jobs and can be used to automate a wide variety of system processes. Among other things, cron jobs can be used to schedule periodic system scans, check for updates, send emails, create backups, and so much more.
Scheduling tasks this way may seem a bit daunting at first but don’t worry because it’s not as difficult as it may seem. In this article, we’re going to attempt to describe the process step-by-step in an intuitive manner so you can learn how to use the crontab even if you’re a brand new Linux user.
Everything You Need to Know About Crontab Files
The cron table, better known as the crontab, is basically a text file that contains information related to the scheduling of cron jobs. This utility can be used to both create and edit existing tasks. There are two main types of crontab files you need to be aware of – user-specific and system-wide.
Just as their name suggests, user-specific crontab files are created separately for each individual user. These files can be stored in one of two places depending on which Linux distribution you are running. On Debian-based systems, you will find the files in the directory labeled /var/spool/cron. Meanwhile, on RedHat-based systems crontabs are found inside a slightly different directory known as /var/spool/cron/crontabs.
As far as system-wide crontab files are concerned, these are located in the directory labeled /etc/cron.d. While user-specific crontab files can be edited by the current working user, system-wide files can only be edited by system administrators.
Crontab Syntax and Operators
The crontab syntax is composed of six different fields separated by a single space. The first five fields contain values that define the time and date of the cron job you want to schedule. Meanwhile, the sixth field simply contains the commands or scripts that need to be executed. To give you a bit of a visual indication, the syntax will look something like this:
* * * * * command(s) - - - - - | | | | | | | | | ----- Day of week (0 - 7) (Sunday=0 or 7) | | | ------- Month (1 - 12) | | --------- Day of month (1 - 31) | ----------- Hour (0 - 23) ------------- Minute (0 - 59)
The asterisk (*) operator means ‘always’ or ‘every’ in the context of this syntax. For example, if you place asterisk in the Day of Week field, the scheduled cron job will be executed on that particular day of every single week.
The hyphen (-) operator can be used to specify a certain range of values. For example, if you have the range 1-10 in the Day of Month field, the cron job will only run during the first 10 days of each month.
The comma operator (,) comes in handy when you want to schedule a list of tasks that must be repeated at certain intervals. For example, if you type 2,4,6 in the Day of Week field, the cron job will only run on Tuesdays, Thursdays, and Saturdays. The comma operator can be used in conjunction with the hypen operator to define multiple ranges such as 10-15, 25-30. If you have those ranges in the Day of Month field, the task will only run between the 10th and 15th day and then between the 25th and 30th day of each month.
The slash (/) operator is similar to comma in the sense that it can also be used to schedule cron jobs that must be executed at certain intervals. However, it works a little bit differently. For example, if you type */3 in the Day of Month field, the task will run every three days, starting with the first day of the month. It’s essentially the equivalent of typing 0,3,6,9,12 etc. The asterisk before the slash can be replaced by a range of values, such as 2-20. For example, if you have 2-20/3 in the Day of Month field, the task will run every three days starting with the second day of the month and will stop running after the 20th day.
System-wide Crontab Files
The syntax we discussed up until now only works with user-specific crontab files. If you’re working with system-wide files, you’ll need to use a slightly different syntax that includes and additional field. This seventh field is used to specify which user or users the task should run under and must be added before the command field. In other words, the syntax for system-wide crontab files should like a little something like this:
* * * * * <username> command(s)
How to Use Predefined Macros
Knowing how to use Cron operators can come in handy but you can make things even easier for you by also working with macros. Macros are shortcuts that can be used instead of the five aforementioned fields to quickly schedule cron jobs that don’t require very specific values. Here are the most commonly used crontab macros you need to know:
@reboot – Can be used to schedule cron jobs to run every time you boot up your system.
@hourly – Can be used to schedule cron jobs to run once every hour. The end result will be the same as typing in 0 * * * * in the five syntax fields.
@daily – Can be used to schedule cron jobs to run once per day starting at midnight. The end result will be the same as typing in 0 0 * * * in the five syntax fields.
@weekly – Can be used to schedule cron jobs to run once a week starting on Sunday at midnight. The end result will be the same as typing in 0 0 * * 0 in the five syntax fields.
@monthly – Can be used to schedule cron jobs to run once a month starting on the first day of the month at midnight. The end result will be the same as typing in 0 0 1 * * in the five syntax fields.
@yearly – Can be used to schedule cron jobs to run once a year starting at midnight on January 1st. The end result will be the same as typing in 0 0 1 1 * in the five syntax fields.
How to Use the Crontab Command
Crontab files can be edited manually but it’s generally a better idea to use the crontab command instead, particularly if you’re not a very experienced user and still trying to figure things out. Using the crontab command allows you to not just edit files but also add and remove cron jobs or simply view your currently scheduled tasks in a simplified manner. You have a number of options to work with in the crontab command. Here are the main ones you’ll need to keep in mind:
crontab – e: This option allows you to create a new crontab file or edit one that already exists.
crontab – l: This option allows you to display the contents of the current crontab file you’re working with.
crontab – r: This option allows you to remove the current crontab file.
crontab – i: This option is identical to the previous one but also shows you a prompt before removing the current crontab file.
crontab – u <username>: This option can be used to edit the crontab file of the specified user. You’ll need to have system administrator privileges in order to use this option.
A Few Things to Note About Crontab Variables
The crontab utility automatically assigns certain variables for your working environment that you may want to be aware of. These variables can be modified but if you’re still new to using cron jobs you should probably stick to the default values. At least until you’ve become more accustomed to the utility. The variables that you should keep in mind are:
- PATH=/usr/bin:/bin – This is the default path set automatically by the crontab. If you want to use a different path simply modify the cron $PATH
- /bin/sh – This is the default shell set automatically by the crontab. Change the SHELL variable if you want to set a different shell.
- Any command you are typing in the crontab will be automatically invoked from the home directory of the user you are currently working under. Certain settings in the crontab can be used to override the HOME
- All email notification are automatically sent to the crontab’s current owner by default. This behavior can be overridden by using the MAIL TO variable followed by the email address you want to use instead. The comma operator can be used alongside the MAIL TO variable to create a list of email addresses that will receive the notifications. Alternatively, if you don’t want to receive any notifications at all, you can simply leave the variable empty, e.g. MAIL TO= “”.
A Few Things to Note About Crontab Restrictions
If you’re a system administrator you can easily allow or deny other users access to the crontab. This can be done by modifying two files labeled /etc/cron.allow and /etc/cron.deny. Each of these two files features a list of all usernames that have access to the system.
Best Linux Distros for Developers and Programmers as of 2020
Linux might not be the preferred operating system of most regular users, but it’s definitely the go-to choice for the majority of developers and programmers. While other operating systems can also get the job done pretty well, Linux is a more specialized OS that was…
How to Install IntelliJ IDEA on Ubuntu
If you are looking for a convenient way to manage Java applications on Ubuntu, the solution may lie in the IntelliJ IDEA development platform. An excellent advantage of this option is that it is relatively suitable for beginners, but experienced users will also find it…