如何在centos 7中启动elasticsearch作为服务运行?

时间:2016-06-20 04:54:04

标签: elasticsearch service centos

我已经在centos 7中安装了elasticsearch 2.3.3但是关闭终端后弹性搜索插件头会自动关闭,但我想继续在后台运行。请给我有用的答案。

1 个答案:

答案 0 :(得分:0)

您可以通过两种方式在后台运行它,

1. Nohup 
2. Creating service script and put it in init.d folder

NOHUP

例如: nohup ./bin/elasticsearch

服务脚本

使用以下脚本

#!/bin/bash
### BEGIN INIT INFO
# Provides:          Elasticsearch
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Runs elasticsearch daemon
# Description: Runs the elasticsearch daemon as a non-root user
### END INIT INFO

# Process name
NAME=elasticsearch
DESC="Elasticsearch"
PROG="/etc/init.d/elasticsearch"

# Configure location of Elasticsearch bin
ELASTICSEARCH_BIN=/opt/elasticsearch-2.3.0/bin

# PID Info
PID_FOLDER=/var/run/elasticsearch/
PID_FILE=/var/run/elasticsearch/$NAME.pid
LOCK_FILE=/var/lock/subsys/$NAME
PATH=/bin:/usr/bin:/sbin:/usr/sbin:$ELASTICSEARCH_BIN
DAEMON=$ELASTICSEARCH_BIN/$NAME

# Configure logging location
ELASTICSEARCH_LOG=/var/log/elasticsearch.log

# Begin Script
RETVAL=0

if [ `id -u` -ne 0 ]; then
        echo "You need root privileges to run this script"
        exit 1
fi

# Function library
. /etc/init.d/functions

start() {
        echo -n "Starting $DESC : "

        pid=`pidofproc -p $PID_FILE elasticsearch`
        if [ -n "$pid" ] ; then
                echo "Already running."
                exit 0
        else
        # Start Daemon
                if [ ! -d "$PID_FOLDER" ] ; then
                        mkdir $PID_FOLDER
                fi
                daemon --user=$DAEMON_USER --pidfile=$PID_FILE $DAEMON 1>"$ELASTICSEARCH_LOG" 2>&1 &
                sleep 2
                pidofproc node > $PID_FILE
                RETVAL=$?
                [[ $? -eq 0 ]] && success || failure
                echo
                [ $RETVAL = 0 ] && touch $LOCK_FILE
                return $RETVAL
        fi
}

reload()
{
    echo "Reload command is not implemented for this service."
    return $RETVAL
}

stop() {
        echo -n "Stopping $DESC : "
        killproc -p $PID_FILE $DAEMON
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && rm -f $PID_FILE $LOCK_FILE
}

case "$1" in
  start)
        start
        ;;
  stop)
        stop
        ;;
  status)
        status -p $PID_FILE $DAEMON
        RETVAL=$?
        ;;
  restart)
        stop
        start
        ;;
  reload)
        reload
        ;;
  *)
        # Invalid Arguments, print the following message.
        echo "Usage: $0 {start|stop|status|restart}" >&2
        exit 2
        ;;
esac

sudo chmod + x /etc/init.d/elasticsearch
sudo update-rc.d elasticsearch默认值96 9

sudo /etc/init.d/elasticsearch restart