Crontab Format in Linux
Linux crontab have six fields. 1-5 fields denotes time and 6’th fields are used for command/script.
[Minute] [hour] [Day_of_the_Month] [Month_of_the_Year] [Day_of_the_Week] [command]
How to Add/Edit Crontab
To add or update job in crontab, use below command. It will open crontab file in editor where job can be added/updated.
# crontab -e
By default it will edit crontab entries of current logged in user. To edit other user crontab use command as below
# crontab -u username -e
Change EDITOR environment variable to change your default editor.
How to List Crontab
To view crontab entries of current user use following command .
# crontab -l
To view crontab entries of other user use following command .
# crontab -u username -l
Crontab Examples:
每天凌晨两点备份数据库:
0 2 * * * /bin/sh backup.sh
每天早上5点和下午5点执行指定程序:
0 5,17 * * * /scripts/script.sh
每分钟执行一次指定程序:
* * * * * /scripts/script.sh
每隔十分钟执行一次指定程序:
*/10 * * * * /scripts/script.sh
每隔一小时执行一次PHP程序:
0 */1 * * * /usr/bin/php /home/wwwroot/script/curl.php >> /home/wwwroot/script/log.txt