Swipe left or right to navigate to next or previous post
Cron also known as cron jobs is a built-in software utility that is used to schedule a time-based job in Unix-like computer operating systems. Users setup and maintain software environments to use cron to schedule jobs which could be either commands or shell scripts to run periodic tasks at fixed time, date or interval. It automates system administration, system maintenance tasks or repetitive like sending the register email, taking database backup, sending notifications.
Crontab or cron table file handles actions of cron. The crontab is a configuration file which specifies shell commands to run periodic tasks at a given schedule. Each user can have their own individual crontab files. There is also a system-wide crontab file usually located in /etc.
You can edit crontab by running following commands for current user
crontab -e
For sudo users
sudo crontab -e
You can use -u pre {username} to specify which users crontab to edit
sudo crontab -u username -e
All users have their own crontab. You can edit the root's crontab by using sudo
crontab -l sudo crontab -l # for sudo users sudo crontab -u username -l # for username
Each line in crontab expects a cron expression mad of five fields which represent the time to execute the command, followed by a shell command to execute and optional output
a b c d e /directory/command output
So, the parts of a cron command are:
Field | Possible Values | Syntax | Description |
[a] – Minute | 0 – 59 | 2 * * * * | The cron job is initiated every time the system clock shows 10 in the minute’s position. |
[b] – Hour | 0 – 23 | 0 2 * * * | The cron job runs any time the system clock shows 2am (2pm would be coded as 14). |
[c] – Day | 1 – 31 | 0 0 2 * * | The day of the month is 2 which means that the job runs every 2nd day of the month. |
[d] – Month | 0 = none, 1 = January 12 = December | 0 0 0 2 * | The numerical month is 2 which determines that the job runs only in February. |
[e] – Day of the Week | 0 = Sunday and 7 = Sunday | 0 0 0 * 2 | 2 in the current position means that the job would only run on Tuesday. |
The * * * * * symbol means to run on every occurrence. It means * on every minute * of every hour * on each day of the month * for each month * every day of the week
To schedule a task to execute every 30 seconds is not possible by time parameters, But it can be done by scheduling same task as below
* * * * * /path/to/executable/script.py * * * * * sleep 30; /path/to/executable/script.py
* * * * * /path/to/script
*/15 * * * * /path/to/script
0 */6 * * * /path/to/executable/script.py
0 2 * * * /bin/sh backup.sh
You can specify multiple timestamps by comma separated
0 11,23 * * * /path/to/executable/script.py
1 0 * * * /path/to/script
10 1 * * 0 /path/to/script
15 2 1 * * /path/to/script
* * * jan,may,aug * /script/script.sh
0 17 * * sun,fri /script/script.sh
0 4,17 * * sun,mon /path/to/executable/script.py
@yearly is similar to 0 0 1 1 * as it will run at first minute of every year. It can be used to send the year greetings to the customers or friends.
@yearly /path/to/executable/script.py
@monthly timestamp is similar to 0 0 1 * * as it will execute in the first minute of the month. It can be used to automate the tasks like paying bills or send invoice to the customers.
@monthly /path/to/executable/script.py
@weekly timestamp is similar to 0 0 * * mon as it will execute a task in the first minute of the week. It may be useful to do weekly tasks like the cleanup of the system etc.
@weekly /bin/script.sh
@daily timestamp is similar to 0 0 * * * as it will execute a task in the first minute of every day. It may be useful to do daily task.
@daily /path/to/executable/script.py
@hourly timestamp is similar to 0 * * * * as it will execute a task in the first minute of every hour, It may be useful to do hourly tasks.
@hourly /path/to/executable/script.py
@reboot is useful for those tasks which you want to run on your system startup to start tasks in the background automatically when system starts.
@reboot /path/to/executable/script.py
We can run the python command using the python virtual environment. There is no need to activate the virtual environment or create a script to activate it. Python interpreter can directly be invoked using the full path from virtual environment It will automatically use the packages associated with that from virtual environment location.
* * * * * /fullpath/to/venv/bin/python /path/to/executable/script.py
* * * * * MY_ENV_VAR="test" MY_ENV_VAR_2="bvariable" /path/to/executable/script.sh
We can run multiple commands in a row by Concatenation with &&. If any scripts fails(returns non-zero exit code), it will exit and will not run the next command
* * * * * /script1.sh && /script2.sh && /script3.sh
or
* * * * * /script1.sh; /script2.sh; /script3.sh
* * * * * /path/to/executable/script.sh > /path/to/log/file.txt 2>&1
* * * * * /path/to/executable/script.sh > /dev/null 2>&1
The location of log file differ by linux distribution. The log file is mostly found at: * The journalctl output systemd's cron.service * Log files in /var/log/ like /var/log/cron
journalctl -u cron # View systemd logging journalctl -u cron -f # Tail systemd log for cron
tail -f /var/log/cron
Cron Schedules edited with crontab -e are stored in /var/spool/cron and they should never be modified directly
crontab -l > cron-backup.txt
crontab -r
crontab cron-backup.txt