Understanding cron command in Linux Ubuntu – what is cron job in Linux. Learn how to set up scheduled tasks in Linux or schedule cron job in Ubuntu Linux. Learn how to install cron and configure cron Ubuntu to schedule a task in Ubuntu Linux Systems.
Cron
cron searches its spool area (/var/spool/cron/crontabs) for crontab files (which are named after accounts in /etc/passwd); crontabs found are loaded into memory. Note that crontabs in this directory should not be accessed directly – the crontab command should be used to access and update them.
cron also reads /etc/crontab, which is in a slightly different format. In Debian, the content of /etc/crontab is predefined to run programs under /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly and /etc/cron.monthly. This configuration is specific to Debian.
Additionally, in Debian, cron reads the files in the /etc/cron.d directory. cron treats the files in /etc/cron.d as in the same way as the /etc/crontab file (they follow the special format of that file, i.e. they include the user field). However, they are independent of /etc/crontab: they do not, for example, inherit environment variable settings from it. This change is specific to Debian. Like /etc/crontab, the files in the /etc/cron.d directory are monitored for changes. In general, the system administrator should not use /etc/cron.d/, but use the standard system crontab /etc/crontab.
/etc/crontab and the files in /etc/cron.d must be owned by root, and must not be group- or other-writable. In contrast to the spool area, the files under /etc/cron.d or the files under /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly and /etc/cron.monthly may also be symlinks, provided that both the symlink and the file it points to are owned by root. The files under /etc/cron.d do not need to be executable, while the files under /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly and /etc/cron.monthly do, as they are run by run-parts.
Installing cron on an Ubuntu
Before installing cron on an Ubuntu machine, update the computer’s local package index:
sudo apt upgrade
sudo apt update
sudo apt install cron
Now execute the following line of command to make sure cron set to run in the background:
sudo systemctl enable cron
Using Cron
Cron jobs are recorded and managed in a special file known as a crontab. Each user profile on the system can have their own crontab where they can schedule jobs, which is stored under /var/spool/cron/crontabs/.
To schedule a job, you just need to open up your crontab for editing and add a task written in the form of a cron expression. The syntax for cron expressions can be broken down into two elements: the schedule and the command to run.
The tasks scheduled in a crontab are structured like this:
minute hour day_of_month month day_of_week command_to_run
Here’s a functional example of a cron expression. This expression runs the command ping 8.8.8.8 every Tuesday at 5:30 PM:
30 17 * * 2 ping 8.8.8.8
To define the time you can provide concrete values for minute (m), hour (h), day of month (dom), month (mon), and day of week (dow) or use ‘*’ in these fields (for ‘any’). Notice that tasks will be started based on the cron’s system daemon’s notion of time and timezones. Output of the crontab jobs (including errors) is sent through email to the user the crontab file belongs to (unless redirected).
For example, you can run a backup of all your user accounts at 5 a.m every week with:
0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
NOTE: The schedule parameters of the cron syntax is divided into 5 different fields. These aer always written in the following order:
minute 0-59
hour 0-23
Day of the month 1-31
month 1-12 or JAN-DEC
Day of the week 0-6 or SUN-SAT
A field may be an asterisk (*), which always stands for “first-last”. Ranges of numbers are allowed. Ranges are two numbers separated with a hyphen. The specified range is inclusive. For example, 8-11 for an “hours” entry speci fies execution at hours 8, 9, 10 and 11.
Lists are also allowed. A list is a set of numbers (or ranges) separated by commas. Examples: “1,2,5,9”, “0-4,8-12”.
Names can also be used for the “month” and “day of week” fields. Use the first three letters of the particular day or month (case doesn’t matter). Ranges or lists of names are not allowed.
The “sixth” field (the rest of the line) specifies the command to be run. The entire command portion of the line, up to a newline or % character, will be executed by /bin/sh or by the shell specified in the SHELL variable of the crontab file. Percent-signs (%) in the command, unless escaped with backslash (\), will be changed into newline characters, and all data after the first % will be sent to the command as standard input. There is no way to split a single command line onto multiple lines, like the shell’s trailing “\”.
NOTE: The day of a command’s execution can be specified by two fields — day of month, and day of week. If both fields are restricted (i.e., aren’t *), the command will be run when either field matches the current time. For example, “30 4 1,15 * 5” would cause a command to be run at 4:30 am on the 1st and 15th of each month, plus every Friday. One can, however, achieve the desired result by adding a test to the command.
Instead of the first five fields, one of eight special strings may appear:
1. @reboot Run once, at startup.
2. @yearly Run once a year, “0 0 1 1 *”.
3. @annually (same as @yearly)
4. @monthly Run once a month, “0 0 1 * *”.
5. @weekly Run once a week, “0 0 * * 0”.
6. @daily Run once a day, “0 0 * * *”.
7. @midnight (same as @daily)
8. @hourly Run once an hour, “0 * * * *”.
crontab
crontab is the program used to install, deinstall or list the tables used to drive the cron daemon. Each user can have their own crontab, and though these are files in /var/spool/cron/crontabs, they are not intended to be edited directly.
If the /etc/cron.allow file exists, then you must be listed (one user per line) therein in order to be allowed to use this command. If the /etc/cron.allow file does not exist but the /etc/cron.deny file does exist, then you must not be listed in the /etc/cron.deny file in order to use this command.
If neither of these files exists, then depending on site-dependent configuration parameters, only the super user will be allowed to use this command, or all users will be able to use this command.
If both files exist then /etc/cron.allow takes precedence. Which means that /etc/cron.deny is not considered and your user must be listed in /etc/cron.allow in order to be able to use the crontab.
Note that regardless of the existance of any of these files, the root administrative user is always allowed to setup a crontab.
When the -u option is given, it specifies the name of the user whose crontab is to be used (when listing) or modified (when editing). If this option is not given, crontab examines “your” crontab, i.e., the crontab of the person executing the command.
The cron is started automatically from /etc/init.d on entering multi-user runlevels.
You can edit your crontab with the following command:
crontab -e
If running the crontab command for the first time, it will prompt you to select a default text editor to use when editing the crontab. After making your selection, you’ll be taken to a new crontab containing some commented-out instructions on how to use the crontab.