Introduction
In Linux, The crond service is one of the tools used to implement periodic scheduled tasks. The crond service can execute the set commands and scripts at the specified time. The function is similar to the "scheduled tasks" in windows ". In a multi-user and multi-task linux system, each user can set their own crontab, in the/var/spool/cron/directory, a file with the same name as the user who created the scheduled task is created, the file content is the scheduled task created by the user using the "crontab-e" command. For each scheduled task, crond records the execution logs to the/var/log/cron log file. Through this file, you will know that at a certain time, which command is executed by the crond program. As a system administrator, users of crontab can be limited to/etc/cron. allow and/etc/cron. deny files are used to set permissions. Generally, only one file is retained. If only/etc/cron is retained. the deny file contains a user name in a row, indicating that the user in the file cannot use the scheduled task function, other users who are not in this file can use the task scheduler function.
Command Format
We use the crontab tool to set a scheduled task. Common options of crontab are "-e" and "-l ".
-E: edit, which indicates editing the scheduler task list.
-L: list, indicating to list the scheduled tasks
[Root @ zhaochj ~] # Crontab-e
Scheduled tasks involve two elements: time and instruction. That is, the time at which commands will be executed.
Syntax format of cron: five time fields, one command field
Hour, day, month, and week commands
Time Value range:
Field allowed values
-------------------
Minute 0-59
Hour 0-23
Day of month 1-31
Month 1-12 (or names, see below)
Day of week 0-7 (0 or 7 is Sun, or use names)
Some special symbols can be used in these five time fields:
*: Indicates any time
,: Indicates time division. For example, if the value in the "Minute" field is "15th", it indicates the first minute and minutes.
-: Indicates a time period. For example, if the value in the "Hour" field is "2-5", it indicates the time range from.
/N: n indicates an integer. For example, if the value in the field "day" is "*/1", the command is executed every other day.
Time period examples
43 21 *** run
15 05 *** run
0 17 *** execute
0 17 ** 1 run at every Monday
17 **, 3 run at and on every Sunday, Tuesday, and Wednesday.
0-10 17 1 ** from PM to PM every other 1 minute
0 0*1 on Monday 1 and 15
42 4 1 ** executed at on the first day of the month
0 21 ** 1-6 from Monday to Saturday
0, 10, 20, 30, 40, 50 *** run every 10 minutes
*/10 * execute every 10 minutes
* 1 *** run every 1 minute from
0 1 * execution
0 */1 * execution at every 1 hour
0 * execution at every 1 hour
2 8-20/3 *** run
30 5, ** executed at on the 1st and 15th of each month
An instance with a backup script and cron used for automatic backup
The backup script is as follows:
[Root @ zhaochj ~] # Vim autoback. sh
#! /Bin/sh
#
# Author: Jack. zhao
# Date: 2013-11-29
# Email: 419775240@qq.com
# Description: This script backup file
DATE = $ (/bin/date + % Y % m % d)
BASENAME = $ (basename $1)
[-D/backup] |/bin/mkdir/backup
/Bin/tar-czf/backup/export basename..tar.gz $1>/dev/null 2>/backup/$ BASENAME. error. log
If [$? -Eq 0]
Then
Echo "$1 $ DATE backup successfully">/backup/$ BASENAME. success. log
Else
Echo "ERROR: failure $1 $ DATE backup! ">/Backup/$ BASENAME. error. log
Fi
[Root @ zhaochj ~] # Chmod + x/root/autoback. sh
How to use this script:
[Root @ zhaochj ~] #./Autoback. sh/etc/fstabefsg
When the script is ready, set the scheduled task. If we want to perform an automatic backup for the/home directory, we need to back up the task at 03:15 on the first and 15th of each month:
[Root @ zhaochj ~] # Crontab-e
15 3, **/root/autoback. sh/home