Crontab is a good entry in Wikipedia. Unfortunately, this address cannot be accessed by proxy in China. Although the introduction of Crontab is everywhere, after reading this entry in detail, we still have some gains. The Crontab name is from chronos, an ancient Greek language, and "time". common traps: each SA, DBA, or
Crontab is a good entry in Wikipedia. Unfortunately, this address cannot be accessed by proxy in China.
Although the introduction of Crontab is everywhere, after reading this entry in detail, we still have some gains. The Crontab name comes from "chronos", an ancient Greek saying "time.
Common traps
Every SA, DBA, or Common Unix user may encounter problems when using Crontab for the first time. common errors for running Crontab include:
1) A new Cron JOB is created for testing purposes,The interval must exceed two minutes.Otherwise, the JOB cannot be scheduled. If you must ignore the loading configuration time difference between the two minutes, you can restart Cron Daemon.
2) notes for starting the X Window Program from Crontab: either initialize "DISPLAY =: 0.0" before the program or append the parameter -- display: 0.0 after the application.
3) The % in the command must be escaped: \ %. in my opinion, do not include this parameter in the command line. simply write it to the script and schedule the script.
Actually, I thinkThe most common problem with Crontab is that the environment variable is incorrect.. I often see people in the forum asking:Why is my Crontab created and not executed?? When creating a Cron JOB, many people like to run it in the command line, because the environment variables are automatically brought in with Shell, in Crontab, the JOB cannot be executed because the correct environment variable cannot be found. This small problem is like having a hard time, and I will remember it after a lesson. I just met you two days ago and asked crontab to run a PHP every minute, as follows:
*/1 * php-q/home/owner/www/ci/crond/index. php
I forgot that the php command can be directly typed because it is in my PATH, while crontab does not recognize the PATH of your user, so the correct syntax should be:
*/1 *****/usr/bin/php-q/home/owner/www/ci/crond/index. php
A required skill
After each JOB is executed, the system automatically sends an email to the current system user. It has been a lot of days and may even crash the entire system. Therefore, it is necessary to perform redirection after each JOB command:>/dev/null 2> & 1. The premise is that the commands in the Job need to be properly output, such as appending to a specific log file.
Appendix: The format of Crontab is described as follows:
* Comma (',') specifies the list value. For example: ", 8"
* The range value specified by the hyphen ('-') is "1-6", indicating "1, 2, 4, 5, 6"
* Asterisk ('*') indicates all possible values.
In Linux (open-source systems may all be available), "/" is available. in the Minute field, */15 indicates execution every 15 minutes. this feature is not available on commercial Unix systems, such as AIX.
# Use the hash sign to prefix a comment
# + ---------------- Minute (0-59)
# | + ------------- Hour (0-23)
# | + ---------- Day of month (1-31)
# | + ------- Month (1-12)
# | + ---- Day of week (0-7) (Sunday = 0 or 7)
# |
# ***** Command to be executed
What is the difference between running crontab for root users and normal users?
The root user can specify a user to execute this command, so his statement can be as follows:
18 23 *** owner888/home/owner888/backup _Mysql. Sh # let the user owner888 runMysqlBackup Script
A common user does not specify others' permissions, so he can only run the command himself.
18 23 ***/home/owner888/backup_mysql.sh # Note: Do not use/home/owner888/backup_mysql.sh here ~ /Backup_mysql.sh to replace, crontab is not recognized
Paste the backup script to make it easier for you to remember.
# Vi backup_mysql.sh
#! /Bin/bash
Backup_dir =/home/user/backup/databak
Backup_target_dir =/home/user/backup/db
Backup_logs_dir =/home/user/backup/logs
Db = news
DATE = $ (date + % Y % m % d) # example: 20101005
Echo "start copy mysql data" >>$ backup_logs_dir/$ db $ DATE
Echo "----------- 'date +" % Y-% m-% d % H: % M: % S "'------------------" >>$ backup_logs_dir/$ db $ date
# Cp-R/var/db/mysql/news/home/user/backup/db
Cp-R/var/db/mysql/$ db $ backup_target_dir/
Echo "start zip mysql data" >>$ backup_logs_dir/$ db $ DATE
Echo "------------------------"> $ backup_logs_dir/$ db $ DATE
Cd $ backup_target_dir
# Tar-zcvf/home/user/backup/databak/news20101005.tar.gz news/
Tar-zcvf $ backup_dir/mongodb1_date.tar.gz $ db/> $ backup_logs_dir/$ db $ DATE
If [$? -Eq 0]
Then
Echo "backup succeed" >>$ backup_logs_dir/$ db $ DATE
Scp $ backup_dir/mongodb1_date.tar.gz backup_user @ backup_ip:/home/backup_user
Rm-rf $ backup_target_dir /*
Else
Echo "backup fail" >>$ backup_logs_dir/$ db $ DATE
Fi
# Vi backup_mysql.sh
#! /Bin/bash
Backup_dir =/home/user/backup/databak
Backup_target_dir =/home/user/backup/db
Backup_logs_dir =/home/user/backup/logs
Db = news
DATE = $ (date + % Y % m % d) # example: 20101005
Echo "start copy mysql data" >>$ backup_logs_dir/$ db $ DATE
Echo "----------- 'date +" % Y-% m-% d % H: % M: % S "'------------------" >>$ backup_logs_dir/$ db $ date
# Cp-R/var/db/mysql/news/home/user/backup/db
Cp-R/var/db/mysql/$ db $ backup_target_dir/
Echo "start zip mysql data" >>$ backup_logs_dir/$ db $ DATE
Echo "------------------------"> $ backup_logs_dir/$ db $ DATE
Cd $ backup_target_dir
# Tar-zcvf/home/user/backup/databak/news20101005.tar.gz news/
Tar-zcvf $ backup_dir/mongodb1_date.tar.gz $ db/> $ backup_logs_dir/$ db $ DATE
If [$? -Eq 0]
Then
Echo "backup succeed" >>$ backup_logs_dir/$ db $ DATE
Scp $ backup_dir/mongodb1_date.tar.gz backup_user @ backup_ip:/home/backup_user
Rm-rf $ backup_target_dir /*
Else
Echo "backup fail" >>$ backup_logs_dir/$ db $ DATE
Fi