Technotes

Technotes for future me

Cron

The following shows a job that runs one minute past every hour between 9:01 a.m. and 5:01 p.m.

# crontab -e
SHELL=/bin/bash
MAILTO=root@example.com
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

# Set the hardware clock to keep it in sync with the more accurate system clock
03 05 * * * /sbin/hwclock --systohc

# Perform monthly updates on the first of the month
25 04 1 * * /usr/bin/dnf -y update

Schedule a cron to execute every Thursday at 3 p.m.

00 15 * * Thu /usr/local/bin/mycronjob.sh

Schedule a cron to execute twice a day.

0 5,17 * * * /scripts/script.sh

Schedule a cron to execute on every Sunday at 5 PM.

0 17 * * sun  /scripts/script.sh

Schedule a cron to execute on every 10 minutes.

*/10 * * * * /scripts/monitor.sh

Schedule a cron to execute at regular times during normal business hours

01 09-17 * * * /usr/local/bin/hourlyreminder.sh

Schedule a cron to execute on every 5 minutes during every hour between 8 a.m. and 5:58 p.m.

*/5 08-18/2 * * * /usr/local/bin/mycronjob.sh

Schedule a cron to execute on selected months.

* * * jan,may,aug *  /script/script.sh

Schedule a cron to execute on selected days.

0 17 * * sun,fri  /script/script.sh

Schedule a cron to execute on first sunday of every month.

0 2 * * sun  [ $(date +%d) -le 07 ] && /script/script.sh

Schedule a cron to execute on every four hours.

0 */4 * * * /scripts/script.sh

Schedule a cron to execute twice on every Sunday and Monday.

0 4,17 * * sun,mon /scripts/script.sh

Schedule a cron to execute on every 30 Seconds.

To schedule a task to execute every 30 seconds is not possible by time parameters, But it can be done by schedule same cron twice as below.

* * * * * /scripts/script.sh
* * * * *  sleep 30; /scripts/script.sh

Schedule a multiple tasks in single cron.

To configure multiple tasks with single cron, Can be done by separating tasks by the semicolon ( ; ).

* * * * * /scripts/script.sh; /scripts/scrit2.sh

Schedule tasks to execute on yearly ( @yearly ).

@yearly timestamp is similar to “0 0 1 1 *”. It will execute a task on the first minute of every year

@yearly /scripts/script.sh

Schedule tasks to execute on monthly ( @monthly ).

@monthly timestamp is similar to “0 0 1 * *”. It will execute a task in the first minute of the month.

@monthly /scripts/script.sh

Schedule tasks to execute on Weekly ( @weekly ).

@weekly timestamp is similar to “0 0 1 * mon”. It will execute a task in the first minute of the week

@weekly /bin/script.sh

Schedule tasks to execute on daily ( @daily ).

@daily timestamp is similar to “0 0 * * *”. It will execute a task in the first minute of every day

@daily /scripts/script.sh

Schedule tasks to execute on hourly ( @hourly ).

@hourly timestamp is similar to “0 * * * *”. It will execute a task in the first minute of every hour

@hourly /scripts/script.sh

Schedule tasks to execute on system reboot ( @reboot ).

@reboot is useful for those tasks which you want to run on your system startup. It will be the same as system startup scripts. It is useful for starting tasks in the background automatically.

@reboot /scripts/script.sh

Redirect Cron Results to specified email account.

By default, cron sends details to the current user where cron is scheduled. If you want to redirect it to your other account, can be done by setup MAIL variable like below

# crontab -l
MAIL=blaataap
0 2 * * * /script/backup.sh

Taking backup of all crons to plain text file.

Check current scheduled cron:

# crontab -l
MAIL=blaataap
0 2 * * * /script/backup.sh

Backup cron to text file:

# crontab -l > cron-backup.txt
# cat cron-backup.txt
MAIL=blaataap
0 2 * * * /script/backup.sh

Removing current scheduled cron:

# crontab -r
# crontab -l
no crontab for root

Restore crons from text file:

# crontab cron-backup.txt
# crontab -l
MAIL=blaataap
0 2 * * * /script/backup.sh

Inspired by https://tecadmin.net/crontab-in-linux-with-20-examples-of-cron-schedule/

Last updated on 31 Jan 2021
Published on 8 Jun 2020
Edit on GitHub