|
At work requires a database automatic backup every day at 0:00 so it is necessary to establish a regular task. I chose to add a scheduled task execution shell Crontab files in Linux. shell have a database backup file command.
One. Crontab Introduction
Function crontab command at a certain time interval schedule execution of commands.
two. Check / etc / crontab file
vim / etc / crontab
three. File / etc / crontab job description of each line in the following format:
minute hour day month dayofweek command
minute - an integer from 0 to 59
hour - integer from 0 to 23
day - an integer from 1 to 31 (must be a valid date of a given month)
month - an integer from 1 to 12 (or, as Jan or Feb abbreviated month)
dayofweek - integer from 0-7, 0 or 7 is used to describe Sunday (or Sun or Mon shorthand representation)
command - the command to be executed (available as ls / proc >> / tmp / proc or execute custom script commands)
root represents the root user to run
run-parts is represented followed by a folder, to execute all the scripts folder
For the above statement, the asterisk (*) indicates that all available values. * In the example refers to month, said the monthly execution (need to comply with other restrictions) the command.
Hyphen integer between (-) represents an integer column, for example, 1-4 means that the integer 1,2,3,4
Specified values separated by commas. Such as: 3,4,6,8 represent the four specified integer.
The symbol "/" specified Step setting. "/ " represents the step value. As 0-59 / 2 defined execution once every two minutes. Step value can also be indicated by an asterisk. Such as * / 3 is used to run the specified task to run every three months.
With "#" at the beginning of the comment line, it will not be executed.
If a cron task needs on a regular basis rather than by the hour, day, week, month to execute, you need to add /etc/cron.d directory. All files and directories under the / etc / crontab same syntax, view examples:
# Record the memory usage of the system every monday
# At 3:30 AM in the file / tmp / meminfo
30 3 * * mon cat / proc / meminfo >> / tmp / meminfo
# Run custom script the first day of every month at 4:10 AM
10 4 1 * * /root/scripts/backup.sh
In addition to the root user's crontab configuration user can perform scheduled tasks. All user-defined crontab is stored in the directory / var / spool / cron, the task will be executed to the identity of the creator. To create a crontab as a specific user, the first user to log in, execute the command crontab -e, the system will start specified in the VISUAL or EDITOR the editing software crontab. File contents and / etc / crontab same format. Examples are as follows:
0 3 * * * /home/dbbackup/db1backup.sh backup
0 4 * * * /home/dbbackup/db2backup.sh backup
Showing daily 3:00 to perform /home/dbbackup/db1backup.sh backup, 4 point execution /home/dbbackup/db2backup.sh backup, if it is performed once every five minutes could be changed to:
* / 5 * * * * /home/dbbackup/db2backup.sh backup
When changes need to be saved crontab file will be saved in the following file into / var / spool / cron / username. Filename based on user name and different.
cron service is checked every minute /etc/crontab,/etc/cron.d/,/var/spool/cron change files under. If changes will be downloaded to the memory. Therefore, even if a crontab file is changed, the program does not require a reboot. Recommended custom tasks using crontab -e command to add, after exiting with /etc/init.d/crond restart command to restart crond process, said no official documents to restart the process, but I can not be met without rebooting run the task situation. Beginning not know / etc / crontab file, the run-parts What does it mean, directly to the command in the / etc / crontab format plus not always run, and then I run-parts means followed by the folder.
four. Start crontab services Close
sbin / service crond start // Start Service
/ Sbin / service crond stop // Close Service
/ Sbin / service crond restart // restart the service
/ Sbin / service crond reload // reload the configuration |
|
|
|