Crontab是linux的定时任务的守护进程,精确到分
Centos的安装命令
yum -y install vixie-cron crontabs
ubuntu的安装命令
apt-get install cron
Contos
# 查看cond 状态
service crond status
# 启动cron
service crond start
# 关闭cron
service crond stop
# 重启cron
service crond restart
Ubuntu
# 查看cond 状态
service cron status
# 启动cron
service cron start
# 关闭cron
service cron stop
# 重启cron
service cron restart
命令格式
crontab [-u user] file
usage:
crontab
[-u user]
file
crontab
[-u user] [ -e | -l | -r ]
(default operation is replace, per 1003.2)
-e (edit user's
crontab
)
-l (list user's
crontab
)
-r (delete user's
crontab
)
-i (prompt before deleting user's
crontab
)
-s (selinux context)
minute hour day month week command
# For details see man 4 crontabs
# Example of job definition:
.---------------------------------- minute (0 - 59) 表示分钟
| .------------------------------- hour (0 - 23) 表示小时
| | .---------------------------- day of month (1 - 31) 表示日期
| | | .------------------------- month (1 - 12) OR jan,feb,mar,apr ... 表示月份
| | | | .---------------------- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat 表示星期(0 或 7 表示星期天)
| | | | | .------------------- username 以哪个用户来执行
| | | | | | .------ command 要执行的命令,可以是系统命令,也可以是自己编写的脚本文件
| | | | | | |
* * * * * user-name command to be executed123456789101112
格式示例:
*/1 * * * * service httpd restart
每一分钟 重启httpd服务
0 */1 * * * service httpd restart
每一小时 重启httpd服务
30 21 * * * service httpd restart
每天 21:30 分 重启httpd服务
26 4 1,5,23,28 * * service httpd restart
每月的1号,5号 23 号 28 号 的4点26分,重启httpd服务
26 4 1-21 * * service httpd restart
每月的1号到21号 的4点26分,重启httpd服务
*/2 * * * * service httpd restart
每隔两分钟 执行,偶数分钟 重启httpd服务
1-59/2 * * * * service httpd restart
每隔两分钟 执行,奇数 重启httpd服务
0 23-7/1 * * * service httpd restart
每天的晚上11点到早上7点 每隔一个小时 重启httpd服务
0,30 18-23 * * * service httpd restart
每天18点到23点 每隔30分钟 重启httpd服务
0-59/30 18-23 * * * service httpd restart
每天18点到23点 每隔30分钟 重启httpd服务
59 1 1-7 4 * test 'date +\%w' -eq 0 && /root/a.sh
四月的第一个星期日 01:59 分运行脚本 /root/a.sh ,命令中的 test是判断,%w是数字的星期几
例子命令:
crontab -e -u root
然后 输入我们的
* * * * * 你的命令
内容来源:http://www.gtalent.cn/