Elasticsearch6.6.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設置執行腳本並添加開機啓動

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章