凡事都在事上磨,这样才能精进,成长进步提高! ------ 博客园首页

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

此时任务就在按照编写的频率执行了。可以定时重启系统,定时删除日志,定时检查服务是否正常运行等

posted @   追风fc  阅读(60)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
历史上的今天:
2023-01-31 美剧推介
点击右上角即可分享
微信分享提示