cron检测并启动logstash
Posted daleyzou
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了cron检测并启动logstash相关的知识,希望对你有一定的参考价值。
背景
线上的logstash总是莫名其妙的挂了,我打算写一个定时任务,一分钟去检查一次logstash进程,不存在时就把它启动
步骤
- 编写检测启动脚本
- 让cron定时来调用检测启动脚本
1、编写脚本
第一次完成是这个样子:
#!/usr/bin/env bash
pid_blog=`ps -ef|grep ‘org.logstash.Logstash‘ |grep -v grep|awk ‘{print $2}‘`
if [ "$pid_blog" == "" ];
then
echo "now time is $(date +‘%Y-%m-%d %H:%M:%S.%s‘)"
nohup /home/logstash/logstash-6.4.2/bin/logstash -f /home/logstash/logstash-6.4.2/config/logstash.conf >> /data-big-1/logstash//logstash.log 2>&1 &
echo "已将logstash启动"
else
echo "端口未被占用"
fi
后来发现cron无法启动logstash,原因是cron执行时没有对应的系统环境变量,例如:没有java环境
进一步优化:
使用 env 命令查看系统变量,然后将系统变量 export 进我的脚本中
优化后的脚本:
#!/usr/bin/env bash
export XDG_SESSION_ID=485047
export HOSTNAME=log-platform-tx-es-03
export TERM=xterm
export SHELL=/bin/bash
export JRE_HOME=/opt/jdk1.8.0_211/jdk1.8.0_211/jre
export USER=logstash
export CLASS_PATH=.:/opt/jdk1.8.0_211/lib/dt.jar:/opt/jdk1.8.0_211/lib/tools.jar:/opt/jdk1.8.0_211/jdk1.8.0_211/jre/lib
export PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/opt/jdk1.8.0_211/bin:/opt/jdk1.8.0_211/jdk1.8.0_211/jre/bin
export PWD=/home/logstashscripts
export JAVA_HOME=/opt/jdk1.8.0_211
export LANG=en_US.UTF-8
export SHLVL=5
pid_blog=`ps -ef|grep ‘org.logstash.Logstash‘ |grep -v grep|awk ‘{print $2}‘`
if [ "$pid_blog" == "" ];
then
echo "now time is $(date +‘%Y-%m-%d %H:%M:%S.%s‘)"
nohup /home/logstash/logstash-6.4.2/bin/logstash -f /home/logstash/logstash-6.4.2/config/logstash.conf >> /data-big-1/logstash//logstash.log 2>&1 &
echo "已将logstash启动"
else
echo "端口未被占用"
fi
2、cron 每一分钟调用一次脚本
1、crontab -e 编辑任务
2、添加任务如下
*/1 * * * * flock -xn /tmp/logstash_start.lock -c ‘/home/logstashscripts/logstash_start.sh >> /home/logstashscripts/logstash_start.log 2>&1‘
参考文档:
https://blog.csdn.net/sum__mer/article/details/83387994
https://askubuntu.com/questions/23009/why-crontab-scripts-are-not-working
以上是关于cron检测并启动logstash的主要内容,如果未能解决你的问题,请参考以下文章