一,crontab
二,crontab安装与检查
首先创建一个sh文件在这个目录/usr/local/bin/,填写下面代码
#!/bin/bash
# 重启 Cloudflare 服务
systemctl restart cloudflared
# 记录日志(可选,便于排查问题)
echo "Cloudflare 服务已重启,时间:$(date '+%Y-%m-%d %H:%M:%S')" >> /var/log/restart-cloudflare.log
在运行一边测试可行性
#测试
sudo /usr/local/bin/restart-cloudflare.sh
#查看日志
cat /var/log/restart-cloudflare.log

最后在编写crontab
#编写
sudo nano /etc/crontab
#在这里添加
0 * * * * root /usr/local/bin/restart-cloudflare.sh
#每小时定时重启任务

三,列子
下面是一些Crontab定时任务例子,其中加#的例子来源于Linux中Crontab(定时任务)命令详解及使用教程 – 天乐博客
1)每1分钟执行一次
* * * * *
2)每小时的第3和第15分钟执行
3,15 * * * *
3)在上午8点到11点的第3和第15分钟执行
3,15 8-11 * * *
4)每两天的上午8点到11点的第3和第15分钟执行
3,15 8-11 */2 * *
5)每个星期一的上午8点到11点的第3和第15分钟执行
3,15 8-11 * * 1
6)每晚的21:30重启smb
30 21 * * * /etc/init.d/smb restart
7)每月1、10、22日的4 : 45重启smb
45 4 1,10,22 * * /etc/init.d/smb restart
8)每周六、周日的1:10重启smb
10 1 * * 6,7 /etc/init.d/smb restart
9)每天18:00至23:00,整点和整半点重启smb
0,30 18-23 * * * /etc/init.d/smb restart
10)每星期六的晚上11:00 pm重启smb
0 23 * * 6 /etc/init.d/smb restart
11)每一小时重启smb
* */1 ** * /etc/init.d/smb restart
12)晚上11点到早上7点之间,每一小时重启smb
* 23-7/1 * * *
13)每月的4号与每周一到周三的11点重启smb
0 11 4 * 1-3 /etc/init.d/smb restart
14)一月一号的4点重启smb
0 4 1 1 * /etc/init.d/smb restart
15)每小时的第1分钟执行/etc/cron.hourly目录内的脚本
1 * * * * /etc/cron.hourly
16)每一小时重启smb
0 */1 * * * /etc/init.d/smb restart
#每晚的21:30 重启apache
30 21 * * * /usr/local/etc/rc.d/lighttpd restart
#每月1、10、22日的4 : 45重启apache
45 4 1,10,22 * * /usr/local/etc/rc.d/lighttpd restart
#每周六、周日的1 : 10重启apache
10 1 * * 6,0 /usr/local/etc/rc.d/lighttpd restart
#每天18 : 00至23 : 00之间每隔30分钟重启apache
0,30 18-23 * * * /usr/local/etc/rc.d/lighttpd restart
#每星期六的11 : 00 pm重启apache
0 23 * * 6 /usr/local/etc/rc.d/lighttpd restart
#晚上11点到早上7点之间,每隔一小时重启apache
* 23-7/1 * * * /usr/local/etc/rc.d/lighttpd restart
#每一小时重启apache
* */1 * * * /usr/local/etc/rc.d/lighttpd restart
#每月的4号与每周一到周三的11点重启apache
0 11 4 * mon-wed /usr/local/etc/rc.d/lighttpd restart
#一月一号的4点重启apache
0 4 1 jan * /usr/local/etc/rc.d/lighttpd restart
#每半小时同步一下时间
*/30 * * * * /usr/sbin/ntpdate cn.pool.ntp.org
#每两个小时重启一次apache
0 */2 * * * /sbin/service httpd restart
#每天7:50开启ssh服务
50 7 * * * /sbin/service sshd start
#每天22:50关闭ssh服务
50 22 * * * /sbin/service sshd stop
#每月1号和15号检查/home 磁盘
0 0 1,15 * * fsck /home
#每小时的第一分执行 /home/bruce/backup这个文件
1 * * * * /home/bruce/backup
#每周一至周五3点钟,在目录/home中,查找文件名为*.xxx的文件,并删除4天前的文件。
00 03 * * 1-5 find /home "*.xxx" -mtime +4 -exec rm {} \;
#每月的1、11、21、31日是的6:30执行一次ls命令
30 6 */10 * * ls
四,检查 crontab 服务状态
Ubuntu/Debian:
systemctl status cron # 查看状态
systemctl start cron # 启动服务
systemctl enable cron # 开机自启
CentOS/RHEL:
systemctl status crond # 查看状态
systemctl start crond # 若未运行,启动服务
systemctl enable crond # 设置开机自启