Elasticsearch6.6.2设置启动脚本

文章目录

  • 1.设置脚本
  • 2.开机自启

Elasticsearch6.6.2设置启动脚本

1.设置脚本

[root@solang ~]# cd /etc/init.d/

[root@solang init.d]# vi elasticsearch
#!/bin/sh
#description: es
 
export ES_HOME=/usr/local/elasticsearch-6.6.2
export JAVA_HOME=/usr/local/jdk1.8.0_251

case "$1" in
start)
    es_pid=`ps aux| grep Elasticsearch | grep -v grep | awk '{print $2}'`
    if [ "$es_pid" == "" ]; then
        echo "elasticsearch stoped, prepare to start..."
        su solang<<!
        $ES_HOME/bin/elasticsearch -d
!
        while true
        do
            es_pid=`ps aux | grep Elasticsearch | grep -v grep | awk '{print $2}'`
            if [ "$es_pid" == "" ]; then
                sleep 1;
                echo "elasticsearch starting..."
            else
                echo "elasticsearch started,pid is $es_pid"
                break
            fi
        done
    else
        echo "elasticsearch exist,pid is $es_pid"
    fi
    ;;
stop)
    es_pid=`ps aux | grep Elasticsearch | grep -v grep | awk '{print $2}'`
    if [ "$es_pid" == "" ]; then
        echo "elasticsearch not started"
    else
        kill -9 $es_pid
        echo "elasticsearch stoped"
    fi
    ;;
restart)
    es_pid=`ps aux | grep Elasticsearch | grep -v grep | awk '{print $2}'`
    if [ "$es_pid" == "" ]; then
        echo "elasticsearch stoped, prepare to start..."
        su solang<<!
        $ES_HOME/bin/elasticsearch -d
!
        while true
        do
            es_pid=`ps aux | grep Elasticsearch | grep -v grep | awk '{print $2}'`
            if [ "$es_pid" == "" ]; then
                sleep 1;
                echo "elasticsearch starting..."
            else
                echo "elasticsearch started,pid is $es_pid"
                break
            fi
        done
    else
        kill -9 $es_pid
        echo "elasticsearch stoped"
        su solang<<!
        $ES_HOME/bin/elasticsearch -d
!
        while true
        do
            es_pid=`ps aux | grep Elasticsearch | grep -v grep | awk '{print $2}'`
            if [ "$es_pid" == "" ]; then
                sleep 1;
                echo "elasticsearch starting..."
            else
                echo "elasticsearch started,pid is $es_pid"
                break
            fi
        done
    fi
    ;;
*)
    echo "start|stop|restart"
    ;;  
esac
exit $?

需要配置JAVA_HOME。将su solang改为运行ES的非root用户名。

<<间隔符,从标准输入中读入,其中的内容传递到之前的命令。后边的!只是一个间隔符,还可以换为别的符号或者字符串,例:#abc等。另外后边的间隔符!之前最好不要有空格,否则会报错。这个叫Here Document,需要说明的是<<后面的符号必须与结束的符号匹配起来,否则就失效。

grep -v grep在文档中过滤掉包含有grep字符的行。

另外需注意的是grep Elasticsearch使用的是大写Elasticsearch,小写的时候经测试服务器会返回多个进程pid,会有问题,尽量用大写。

[solang100@solang100 ~]$ ps aux | grep elasticsearch | grep -v grep
solang   4961  0.6 53.9 3219148 537084 pts/0  Sl   May08   6:31 /usr/local/jdk1.8.0_251/bin/java -Xms1g -Xmx1g -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -Des.networkaddress.cache.ttl=60 -Des.networkaddress.cache.negative.ttl=10 -XX:+AlwaysPreTouch -Xss1m -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djna.nosys=true -XX:-OmitStackTraceInFastThrow -Dio.netty.noUnsafe=true -Dio.netty.noKeySetOptimization=true -Dio.netty.recycler.maxCapacityPerThread=0 -Dlog4j.shutdownHookEnabled=false -Dlog4j2.disable.jmx=true -Djava.io.tmpdir=/tmp/elasticsearch-1841885597425107477 -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=data -XX:ErrorFile=logs/hs_err_pid%p.log -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintTenuringDistribution -XX:+PrintGCApplicationStoppedTime -Xloggc:logs/gc.log -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=32 -XX:GCLogFileSize=64m -Des.path.home=/usr/local/elasticsearch-6.6.2 -Des.path.conf=/usr/local/elasticsearch-6.6.2/config -Des.distribution.flavor=default -Des.distribution.type=tar -cp /usr/local/elasticsearch-6.6.2/lib/* org.elasticsearch.bootstrap.Elasticsearch -d
solang   4980  0.0  0.0  72140   652 pts/0    Sl   May08   0:00 /usr/local/elasticsearch-6.6.2/modules/x-pack-ml/platform/linux-x86_64/bin/controller

[solang@solang ~]$ jps
4961 Elasticsearch
5526 Jps
# 赋予执行权限
[root@solang init.d]# chmod +x /etc/init.d/elasticsearch

# 启动elasticsearch
[solang@solang init.d]$ service elasticsearch start

2.开机自启

# 添加到开机启动
[root@solang init.d]# chkconfig --add /etc/init.d/elasticsearch

# 开机自启elasticsearch服务
[root@solang init.d]# chkconfig elasticsearch on 

# 关闭elasticsearch开机自启
[root@solang init.d]# chkconfig elasticsearch off 

本文参考:
elasticsearch6.X 及head插件部署(完整版)-开机自启动脚本
elasticsearch设置执行脚本并添加开机启动

你可能感兴趣的:(ES,ES启动脚本,Elasticsearch,Shell)