crontab Introduction
The crontab command is common in UNIX and unix-based operating systems, and is used to set periodically executed instructions. This command reads the instruction from the standard input device and stores it in a "crontab" file for later reading and execution. The word is derived from the Greek Chronos and is intended to be time. Typically, crontab stored instructions are activated by the daemon, Crond often run in the background, checking every minute for scheduled jobs to be performed. This type of assignment is commonly referred to as cron jobs.
Crontab usage
Format of Crontab
F1 F2 F3 f4 f5 program
Where F1 is the minute, F2 represents the Hour, F3 represents the day of the month, the F4 represents the month, and the F5 represents the day of the one week. The program represents the path to execute.
When F1 is *, the PROGRAM,F2 is executed every minute for *, which means that the program is executed hourly, and the rest of the analogy
When F1 is a-b to be executed from the time of the first a minute to the B minute, the F2 is a-b to execute from a to B hours, and the rest by analogy
When F1 is */n, it is executed once every n minutes, F2 is performed once per N-hour intervals for */n, and the rest by analogy
When F1 is a, B, C,... The first A, B, C,... Minutes to execute, F2 for a, B, C,... The first is a, B, c ... An hour to execute, the rest of the analogy
Admin login ssh, enter command crontab-e edit crontab file, enter and save according to the format above.
crontab Example
Perform a/bin/ls every 0 minutes per day of the month:
The code is as follows |
Copy Code |
0 * * * */BIN/LS |
In December, every 6 to 12 o ' Day in the morning, every 20 minutes to perform a/usr/bin/backup:
The code is as follows |
Copy Code |
*/20 6-12 */usr/bin/backup |
Send a letter to admin@3aj.cn from Monday to Friday every 5:00:
The code is as follows |
Copy Code |
0 * * 1-5 mail-s "HI" admin@3aj.cn </tmp/maildata |
Every month at midnight 0:20, 2:20, 4:20 .... Execute echo "Hi 3a Technology Network"
The code is as follows |
Copy Code |
0-23/2 * * echo "Hi 3a |
Between 11 o'clock and 8 in the morning, every two hours, 8 in the morning.
The code is as follows |
Copy Code |
0 23-7/2,8 * * * Date |
In HP Unix, in every 20 minutes, the expression is: 0,20,40 * * * * * * * and cannot be used */n mode, otherwise there will be syntax errors.
Crontab usage is actually very easy to grasp, know how to use crontab, the website and server maintenance plays a great help, such as scheduled backup, timing optimization server.
Speaking of backup, let's look at a timer backup MySQL database using crontab.
mysql_databasename_backup.sh Backup use
The code is as follows |
Copy Code |
#!/bin/bash Extdir= '/home/mysql/backup/'; Extbase= ' auto_databasename_ ' date ' +%f '; echo $extbase; extfile= ' auto_databasename_ ' Date ' +%f '. sql '; echo $extfile; echo "starts backing up data ..." CD $extdir Mysqldump--opt databasename-u Username-ppassword | gzip > $extdir $extfile.gz echo "Deletes backup data from local 7 days ago ..." old_file= ' auto_databasename_ ' date ' +%f '--date= ' 7 days ago '. * ' Echo $old _file RM-RF $extdir $old_file |
Mysql_autoback_cron.sh Daily Polling
The code is as follows |
Copy Code |
* 3 * * */usr/share/jetty6/cron/mysql_databasename_backup.sh >/usr/share/jetty6/cron/mysql_autoback_cron_log.txt |
Add a word to crontab
code is as follows |
copy code |
crontab mysql_autoback_cron.sh |