我使用的是騰訊的云服務器1核心2G記憶體,安裝的有MySQL資料庫,elasticsearch 啟動后剩余的記憶體就捉襟見肘了,為了運行其他服務,需要停止 elasticsearch 服務,這個時候我才發現 elasticsearch 根本就不希望大家停止掉自己【沒有停止服務的命令】這里總結一下:
1. 直接啟動與停止
啟動:
# 切換到 elasticsearch 用戶
[root@tcloud ~]# su elasticsearch
# 一般啟動
bash-4.2$ /usr/local/elasticsearch/bin/elasticsearch
# 后臺啟動
bash-4.2$ /usr/local/elasticsearch/bin/elasticsearch -d
停止:
# 一般啟動 Ctrl c 【很多非后臺啟動的服務都是這樣停止的】
[root@tcloud bin]#
[2021-08-03T17:11:25,733][INFO ][o.e.x.m.j.p.NativeController] Native controller process has stopped - no new native processes can be started
[2021-08-03T17:11:25,735][INFO ][o.e.n.Node ] [M_rq0Xz] stopping ...
[2021-08-03T17:11:25,743][INFO ][o.e.x.w.WatcherService ] [M_rq0Xz] stopping watch service, reason [shutdown initiated]
[2021-08-03T17:11:25,955][INFO ][o.e.n.Node ] [M_rq0Xz] stopped
[2021-08-03T17:11:25,955][INFO ][o.e.n.Node ] [M_rq0Xz] closing ...
[2021-08-03T17:11:25,976][INFO ][o.e.n.Node ] [M_rq0Xz] closed
# 后臺啟動
# 查詢 elasticsearch 的相關執行緒【多個】
[root@tcloud bin]# ps -ef | grep elastic
# 停止所有 elasticsearch 相關執行緒【多個】
[root@tcloud bin]# kill -9 ***
2. 使用PID啟動與停止【當然也可以不用shell腳本 直接使用命令】
2.1 配置
前邊的方法停止的時候查詢到的執行緒ID是多個,這里只用停掉PID即可,我們撰寫一個shell腳本來實作啟動和停止:
# 添加 pid
[root@tcloud ~]# vim /usr/local/elasticsearch/pid
# 寫入pid值
# 我寫的是 831717
# 將 pid 檔案轉到 elasticsearch 用戶下【這個很重要】
[root@tcloud elasticsearch]# chown -R elasticsearch ./pid
[root@tcloud elasticsearch]# chgrp -R elasticsearch ./pid
# 添加 elasticsearch.sh 腳本檔案
[root@tcloud ~]# vim /usr/local/elasticsearch/elasticsearch.sh
elasticsearch.sh 檔案的內容如下:
#!/bin/bash
if [ $# -ne 1 ]
then
echo "args number is error!!!"
exit
fi
case $1 in
"start")
echo "============啟動ElasticSearch================"
su elasticsearch -c "sh ${ELASTICSEARCH_HOME}/bin/elasticsearch -d -p ${ELASTICSEARCH_HOME}/pid"
;;
"stop")
echo "============停止ElasticSearch================"
kill `cat ${ELASTICSEARCH_HOME}/pid`
;;
*)
echo "args info is error!!!"
;;
esac
# 給 shell 腳本賦權限
[root@tcloud ~]# chmod +x /usr/local/elasticsearch/elasticsearch.sh
2.2 測驗
- 我們不啟動,先停止一下試試 😃
[root@tcloud elasticsearch]# ./elasticsearch.sh stop
============停止ElasticSearch================
./elasticsearch.sh: line 16: kill: (831717) - No such process
- 啟動停止一起測驗
# 啟動
[root@tcloud elasticsearch]# ./elasticsearch.sh start
============啟動ElasticSearch================
# 驗證是否啟動成功
[root@tcloud elasticsearch]# ps -ef | grep elastic
root 2082 16917 0 16:25 pts/2 00:00:00 su elasticsearch
elastic+ 2083 2082 0 16:25 pts/2 00:00:00 bash
elastic+ 17031 1 9 17:30 ? 00:00:22 /usr/local/java/bin/java -Xms256m -Xmx256m -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -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.6IpCYVwq -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 -Des.path.conf=/usr/local/elasticsearch/config -Des.distribution.flavor=default -Des.distribution.type=tar -cp /usr/local/elasticsearch/lib/* org.elasticsearch.bootstrap.Elasticsearch -d -p /usr/local/elasticsearch/pid
elastic+ 17053 17031 0 17:30 ? 00:00:00 /usr/local/elasticsearch/modules/x-pack-ml/platform/linux-x86_64/bin/controller
root 17979 2848 0 17:34 pts/1 00:00:00 grep --color=auto elastic
# 停止并驗證【還有一個elasticsearch的執行緒 但實際上已經關閉了】
[root@tcloud elasticsearch]# ./elasticsearch.sh stop
============停止ElasticSearch================
[root@tcloud elasticsearch]# ps -ef | grep elastic
root 2082 16917 0 16:25 pts/2 00:00:00 su elasticsearch
elastic+ 2083 2082 0 16:25 pts/2 00:00:00 bash
root 18118 2848 0 17:35 pts/1 00:00:00 grep --color=auto elastic
3. 總結
elasticsearch.sh 這個腳本修改后可以用到很多服務的啟動停止上,比如大資料集群、多個jar檔案等,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/291806.html
標籤:其他
