Brief Introduction
Linux crontab and Windows task schedules are very similar. Crontab can be used to perform tasks on a regular basis in the system. For example: Write a reptile needs to be executed every morning at eight, you can use the crontab; the Tomcat server you install needs to be restarted every morning, or you can use the crontab. In short, almost all of the scheduled tasks, we can use crontab this tool to complete. installation
Yum Install Cronie
The structure of crontab on Linux
From left to right, in turn:
Minutes Hours [One day of the month] [January of a year] [One day of the week] [Command to execute]
Note: Please pay attention to the range of values for each option. how to Add/edit Crontab Add or update commands in Crontab
Crontab-e
By default, the system edits the crontab command collection for the currently logged-on user. You need to edit the command collection for another user, and you need to use the following command
Crontab-u USERNAME-E
View crontab Command collectionView the crontab command collection for the current system logged-on user
Crontab-l
View a collection of CRONTAB commands for other users
Crontab-u username-l
20 Practical examples of crontab usagePerform Tasks 02:00 every day
0 2 * * */bin/sh backup.sh
Perform tasks 5:00 and 17:00 daily
0 5,17 * * */scripts/script.sh
Perform a task every minute
Normally, we don't have a script that needs to be executed every minute (silently thinking about 12306–)
* * * * */scripts/script.sh * * *
Perform tasks every Sunday 17:00
0 * Sun /scripts/script.sh
Perform one task per 10min
*/10 * * * */scripts/monitor.sh
To perform a task in a specific few months
* * * Jan,may,aug */script/script.sh *
Perform a task on a specific day, eg: Perform a task at 17 points a week, five, Sunday
0 * * sun,fri/script/scripy.sh
Perform a task on the first Sunday of a month
0 2 * * Sun [$ (date +%d)-le] &&/script/script.sh
Perform a task every four hours
0 */4 * * */scripts/script.sh
Perform tasks every Monday and Sunday
0 4,17 * * sun,mon/scripts/script.sh
Perform a task every 30 seconds
We have no way to do it directly by appealing similar examples, because the smallest is 1min. But we can adopt the following method.
* * * * * * * * */scripts/script.sh * * * * * * * * * * * * /scripts/script.sh
Multiple tasks are configured in a single command
* * * * * * */SCRIPTS/SCRIPT.SH; /scripts/scrit2.sh
Perform a task once a year
@yearly/scripts/script.sh
The @yearly is similar to "0 0 1 1 *". It will be executed in the first minute of each year, and usually we can send a new year greeting with this. Perform a task once a month
@yearly/scripts/script.sh
Perform a task once a week
@yearly/scripts/script.sh
Perform a task once a day
@yearly/scripts/script.sh
Perform a task every minute
@yearly/scripts/script.sh
Execution when system reboots
@reboot/scripts/script.sh
Specific accounts that redirect Cron results
By default, Cron will only send the result details to a cron-made user. If you need to send it to another user, you can do it in the following ways:
# crontab-l
mail=bob
0 2 * * * */script/backup.sh
Back up all cron commands to a text file
This is a quick and easy way to recover when we lose the cron command.
Here is a small example of using this approach to restore cron. (See on the line ~)
First: Check the current cron
Mail=rahul
0 2 * * */script/backup.sh
Then: Backup cron to File
# crontab-l > Cron-backup.txt
# cat cron-backup.txt
mail=rahul
0 2 * * */script/backup.sh
Next: Remove the current cron
# crontab-r
# crontab-l
no crontab for Root
Recovery: Recovering from text file
# crontab Cron-backup.txt
# crontab-l
mail=rahul
0 2 * * */script/backup.sh
Author: Leelom
Link: http://www.jianshu.com/p/d93e2b177814
Source: Jianshu
Copyright belongs to the author. Commercial reprint please contact the author to obtain authorization, non-commercial reprint please indicate the source.