安装crontab
Centos 6,7 下的安装
Centos 6,7 下 crontab 的安装有所区别, 用下面的命令安装:
在 CentOS 7 环境下你需要安装 cronie
:
yum install cronie
在 CentOS 6 环境下需要安装 vixie-cron和
cronie
:
yum install vixie-cron
和
yum install cronie
这两个命令的结果是一样的:
.../... ================================================================== Package Arch Version Repository Size ================================================================== Installing: cronie x86_64 1.4.4-12.el6 base 73 k Installing for dependencies: cronie-anacron x86_64 1.4.4-12.el6 base 30 k crontabs noarch 1.10-33.el6 base 10 k exim x86_64 4.72-6.el6 epel 1.2 M Transaction Summary ================================================================== Install 4 Package(s)
打开cron:
service crond start
/bin/systemctl start crond.service
要使它开机自启:
chkconfig crond on //centos 6
systemctl enable crond.service //centos 7
Debian 下的安装没那么多坑:
apt-get install cron
编辑定时任务配置
crontab -e
在打开的配置文件中加入:
0 3 * * * /sbin/reboot
表示每天凌晨3点0分系统重启
其他例子:
# 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) # | | | | | # * * * * * command to be executed 0 0 */2 * * command1 0 0 2-31/2 * * command2 * * */2 * * command3
command1
will be executed at 00:00 on every odd-numbered day (default range with step 2, i.e. 1,3,5,7,...,31)
command2
will be executed at 00:00 on every even-numbered day (i.e. 2,4,6,8,...,30)
command3
is an often made mistake. This will run on every minute of every odd-numbered day.
Note: It should be understood that in this approach
command1
will run on both January 31st and February 1st (2 consecutive days)
Note: It should be understood that in this approach
command2
will skip both January 31st and February 1st and will run only 3 days later.
ctrl+c,:wq 保存退出
重启程序:
/etc/rc.d/init.d/crond stop //centos 6 /etc/rc.d/init.d/crond restart //centos 6 service crond restart /bin/systemctl restart crond.service //centos 7 debian下: /etc/init.d/cron restart
Ok, 完成!