Loading... ## 系统计划任务 ### 1.编辑文件crontab ``` vi /etc/crontab ``` ### 2.设置重启时间 分时日月年 ``` SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root # 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 # | | | | | # * * * * * user-name command to be executed 30 02 * * * root reboot #02:30 每天凌晨2点30分重启系统 * */6 * * * root reboot #每隔6小时重启一次 */30 * * * * root reboot #每隔30分钟重启一次 30 02 1 * * root reboot #每月1日02:30 时重启服务器 0 0 */2 * * root reboot #每2天00点重启一次服务器 ``` ### 3.装载 ``` crontab /etc/crontab ``` ### 4.定时重启服务 ``` systemctl enable crond.service #开启启动 systemctl restart crond.service #重启服务 ``` ## 用户计划任务 ### 1.进入计划文件 ``` crontab -e ``` ### 2.例: ``` # .---------------- 分钟,取值范围为 0-59 # | .------------- 小时,取值范围为 0-23 # | | .---------- 日,取值范围为 1-31 # | | | .------- 月,取值范围为 1-12 # | | | | .---- 星期,取值范围为 0-7,0 和 7 都表示星期日 # | | | | | .-- 要执行的命令 # | | | | | | 0 19 * * * bash /root/a.sh #记得要给a.sh 可执行权限 ``` a.sh ``` #!/bin/bash echo "hello">> /root/test.txt ``` ### 3.删除计划任务 ``` crontab -r ``` ### 4.crontab 不执行的原因 * 检查 `crontab` 服务是否正常 * 检查脚本路径是否绝对路径 * 检查脚本路径前是否添加了 `bash` 或 `/etc/profile;/bin/sh` 如果以上都没问题,那就要通过日志查找问题所在了。 ## 其他 ### 1. 查看定时配置 ``` crontab -l ``` ### 2. 查看系统重启情况 ``` who -b #最近一次重启时间 last reboot #重启历史 ``` ### 3.Cron表达式生成器 https://www.bejson.com/othertools/cron/ Last modification:December 3, 2022 © Allow specification reprint Support Appreciate the author WeChat Like 0 如果觉得我的文章对你有用,请帮忙点一下上面的广告