logstash服务启动脚本

Posted 桦仔

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了logstash服务启动脚本相关的知识,希望对你有一定的参考价值。

logstash服务启动脚本

最近在弄ELK,发现logstash没有sysv类型的服务启动脚本,于是按照网上一个老外提供的模板自己进行修改

#添加用户
useradd logstash -M -s /sbin/nologin
mkdir /var/log/logstash/
chown -R logstash:logstash /var/log/logstash/
chown -R logstash:logstash /usr/local/logstash-2.0.0/





vi /etc/init.d/logstash
#!/bin/bash
#
### BEGIN INIT INFO
# Provides: logstash
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
# Default-Start: 2 3 4 5
# Default-Stop: S 0 1 6
# Short-Description: Logstash
# Description: Starts Logstash as a daemon.
# Author: [email protected]
### END INIT INFO


source /etc/init.d/functions
source /lib/lsb/init-functions

# Process name
NAME=logstash
DESC="Logstash Daemon"

# Location of logstash files
LOCATION="/usr/local/logstash-2.0.0/"   #根据实际安装路径修改
CONFIG_DIR="/usr/local/logstash-2.0.0/"
LOGFILE="/var/log/logstash/logstash.log"

SCRIPTNAME=/etc/init.d/logstash
PIDFILE="/var/run/logstash-agent.pid"
LOCK_FILE=/var/lock/subsys/$NAME


NAMEJAVA=java
DAEMONJAVA=`which java`


# Exit if the package is not installed
if [ ! -x "$DAEMONJAVA" ]; then
{
  echo "Couldn‘t find $NAMEJAVA"
  exit 99
}
fi


start() {
        echo -n "Starting $DESC : "
        if [ "$(ps aux|grep -E "*/usr/local/logstash*" |grep -v grep)" != "" ];then
             echo "$desc already running...."
             exit 0
             
        else
        
        $LOCATION/bin/logstash -f  $CONFIG_DIR/logstash_agent.conf >$LOGFILE 2>&1  &
        RETVAL=$?
        sleep 3
        echo
            if [ "$(ps aux|grep -E "*/usr/local/logstash*" |grep -v grep)" != "" ];then
                echo "$DESC Started "
                [ $RETVAL -eq 0 ] && touch $LOCK_FILE
                return $RETVAL
            fi
        
        
        fi
       

}
stop() {
        echo -n $"Stop $DESC: "
        killall $NAMEJAVA
        RETVAL=$?
        sleep 3
        echo
        if [ "$(ps aux|grep -Eqi "*/usr/local/logstash*" |grep -v grep)" = "" ];then
                echo "$DESC Stoped "
                [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$NAME $PIDFILE
                return $RETVAL
        fi
       
}
restart() {
        stop
        start
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  restart)
        restart
        ;;
  status)
        status $NAMEJAVA
        RETVAL=$?
        ;;
   *)
        echo $"Usage: $0 {start|stop|restart|status}"
        RETVAL=1
esac
exit $RETVAL
EOF






chmod +x /etc/init.d/logstash
#设置开机启动
chkconfig --add logstash
chkconfig logstash on

 

如有不对的地方,欢迎大家拍砖o(∩_∩)o 

本文版权归作者所有,未经作者同意不得转载。

以上是关于logstash服务启动脚本的主要内容,如果未能解决你的问题,请参考以下文章

cron检测并启动logstash

ELK技术栈之-Logstash详解

ELK之logstash长久运行

Logstash设置服务启动加载自定义的管道配置

Logstash设置服务启动加载自定义的管道配置

sh Unbounce脚本片段,用于在零停机时间内重新启动HAProxy