1.安装定时任务
yum install -y vixie-cron
如果安装了就不需要安装,阿里云服务器默认就安装了。
2.定时任务命令
Centos 7 以下版本
service crond start //开启定时任务
service crond stop //关闭定时任务
service crond restart //重启定时任务
service crond status //查看定时任务运行状态
Centos 7 以上版本
systemctl start crond.service //开启定时任务
systemcrl stop crond.service //关闭定时任务
systemctl restart crond.service //重启定时任务
systemctl status crond.service //查看定时任务运行状态
crontab -e //弹出计划任务编辑文件
crontab -l //列出所有的计划任务
crontab -r //删除计划任务文件
3.定时任务命令格式
* * * * * command
分 时 日 月 周 命令
第一列表示分钟 1~59分钟 每分钟用*或者*/1表示
第二列表示小时 1~23 (0表示0)
第三列表示日期 1~31
第四列表示月份 1~12
第五列表示星期 0~6(0表示星期天)
第六列表示要运行的命令
4.案列
//每分钟往文件中写入内容
*/1 * * * * echo 'Linux计划任务' >> /usr/local/nginx/html/demo.txt
//每晚的31:30重启apache
30 21 * * * /usr/local/apache/bin/apachectl restart
//每月1,10.22日的4:55重启apacha
55 4 1,10,22 * * /usr/local/apache/bin/apachectl restart
//每周六,周日的1:30重启apache
30 1 * * 6,0 /usr/local/apache/bin/apachectl restart
//使用curl每分钟运行PHP文件
*/1 * * * * curl https://gyusj.com/index.php
//使用wget每分钟运行PHP文件
*/1 * * * * wget https://gyusj.com/index.php