Linux服务器Crontab定时任务配置
Linux服务器Crontab定时任务配置
1、检查linux系统是否有crontab
rpm -qa | grep crontab
2、如果未安装进行安装
yum -y install vixie-cron yum -y install crontabs
3、crontab启动、停止、运行状态
systemctl start crond 启动 systemctl stop crond 停止 systemctl status crond 查看运行状态 systemctl restart crond 重新启动
4、查看crontab下有无定时任务
crontab -l
5、新增定时任务
crontab -e
执行命令后输入内容填写任务执行频率、任务脚本:
*/1 * * * * sh /task/task.sh >> /task/task.log # 每分钟执行task.sh
6、编写Shell任务脚本
status=$(curl -s -o /dev/null -w "%{http_code}" http://127.0.0.1/list) # echo $status >> task.log success=200 if (($status == $success)); then echo $(date "+%Y-%m-%d %H:%M:%S") ":服务正常" echo $(date "+%Y-%m-%d %H:%M:%S") ":服务正常" >> task.log fi if (($status != 200)); then echo $(date "+%Y-%m-%d %H:%M:%S") ":服务异常,准备重启..." echo $(date "+%Y-%m-%d %H:%M:%S") ":服务异常,准备重启..." >> task.log source /001/java2010/reload.sh fi
7、重启crontab
systemctl restart crond
此时任务就在按照编写的频率执行了。可以定时重启系统,定时删除日志,定时检查服务是否正常运行等
因为相信,所以看见.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
2023-01-31 美剧推介