20 Super Practical Crontab Use Cases
- Execute the task every day at 02:00
0 2 * * * /bin/sh backup.sh
- Execute tasks every day at 5:00 and 17:00
0 5,17 * * * /scripts/script.sh
- Execute a task every minute
Usually, we don't have a script that needs to be executed every minute (silently think of 12306--)
* * * * * /scripts/script.sh
- Execute the task every Sunday at 17:00
0 17 * * sun /scripts/script.sh
- Execute a task every 10 minutes
*/10 * * * * /scripts/monitor.sh
- perform tasks in specific months
* * * jan,may,aug * /script/script.sh
- perform tasks on specific days
0 17 * * sun,fri /script/scripy.sh
Execute tasks at 17:00 every Friday and Sunday
- Run the task on the first Sunday of a month
0 2 * * sun [ $(date +%d) -le 07 ] && /script/script.sh
- Execute a task every four hours
0 */4 * * * /scripts/script.sh
- Perform tasks every Monday and Sunday
0 4,17 * * sun,mon /scripts/script.sh
- Execute a task every 30 seconds
We have no way to execute it directly by appealing a similar example, because the minimum is 1min. But we can do it as follows.
* * * * * /scripts/script.sh
* * * * * sleep 30; /scripts/script.sh
- Multiple tasks configured in one command
* * * * * /scripts/script.sh; /scripts/scrit2.sh
- Perform a task once a year
@yearly /scripts/script.sh
@yearly is something like "0 0 1 1 *". It is executed in the first minute of every year, usually we can send New Year's greetings with this.
- Perform a task once a month
@yearly /scripts/script.sh
- Do a task once a week
@yearly /scripts/script.sh
- Execute a task once a day
@yearly /scripts/script.sh
- Execute a task every minute
@yearly /scripts/script.sh
- Executed when the system restarts
@reboot /scripts/script.sh
- A specific account to redirect
cron results to If you need to send it to other users, you can do it in the following ways:
# crontab -l
MAIL=bob
0 2 * * * /script/backup.sh
- Backup all cron commands to a text file
This is a convenient and quick way to restore when we lose cron commands.
The following is a small example of using this method to restore cron. (Just take a look~)
First: check the current cron
# crontab -l
MAIL=rahul
0 2 * * * /script/backup.sh
Author: LeeLom
Link: https://www.jianshu.com/p/d93e2b177814
Source:
The copyright of Jianshu belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.