Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)

简介: 【4月更文挑战第12天】Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)

我使用的是腾讯的云服务器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 测试

  1. 我们不启动,先停止一下试试 :)
    [root@tcloud elasticsearch]# ./elasticsearch.sh stop
    ============停止ElasticSearch================
    ./elasticsearch.sh: line 16: kill: (831717) - No such process
    
  2. 启动停止一起测试
    ```bash

    启动

    [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文件等。

相关实践学习
以电商场景为例搭建AI语义搜索应用
本实验旨在通过阿里云Elasticsearch结合阿里云搜索开发工作台AI模型服务,构建一个高效、精准的语义搜索系统,模拟电商场景,深入理解AI搜索技术原理并掌握其实现过程。
ElasticSearch 最新快速入门教程
本课程由千锋教育提供。全文搜索的需求非常大。而开源的解决办法Elasricsearch(Elastic)就是一个非常好的工具。目前是全文搜索引擎的首选。本系列教程由浅入深讲解了在CentOS7系统下如何搭建ElasticSearch,如何使用Kibana实现各种方式的搜索并详细分析了搜索的原理,最后讲解了在Java应用中如何集成ElasticSearch并实现搜索。  
目录
相关文章
|
7月前
|
存储 数据采集 搜索推荐
Java 大视界 -- Java 大数据在智慧文旅旅游景区游客情感分析与服务改进中的应用实践(226)
本篇文章探讨了 Java 大数据在智慧文旅景区中的创新应用,重点分析了如何通过数据采集、情感分析与可视化等技术,挖掘游客情感需求,进而优化景区服务。文章结合实际案例,展示了 Java 在数据处理与智能推荐等方面的强大能力,为文旅行业的智慧化升级提供了可行路径。
Java 大视界 -- Java 大数据在智慧文旅旅游景区游客情感分析与服务改进中的应用实践(226)
|
5月前
|
JSON Java 数据格式
java调用服务报错400
java调用服务报错400
143 6
java调用服务报错400
|
5月前
|
JSON Java 数据格式
java调用服务报错415 Content type ‘application/octet-stream‘ not supported
java调用服务报错415 Content type ‘application/octet-stream‘ not supported
358 6
|
Shell
Shell 文件包含
10月更文挑战第5天
182 4
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
383 6
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
Java Maven Windows
使用Java创建集成JACOB的HTTP服务
本文介绍了如何在Java中创建一个集成JACOB的HTTP服务,使Java应用能够调用Windows的COM组件。文章详细讲解了环境配置、动态加载JACOB DLL、创建HTTP服务器、实现IP白名单及处理HTTP请求的具体步骤,帮助读者实现Java应用与Windows系统的交互。作者拥有23年编程经验,文章来源于稀土掘金。著作权归作者所有,商业转载需授权。
421 2
使用Java创建集成JACOB的HTTP服务
|
8月前
|
分布式计算 搜索推荐 算法
Java 大视界 -- Java 大数据在智慧养老服务需求分析与个性化服务匹配中的应用(186)
本篇文章探讨了Java大数据技术在智慧养老服务需求分析与个性化服务匹配中的应用。通过整合老年人健康数据与行为数据,结合机器学习与推荐算法,实现对老年人健康风险的预测及个性化服务推荐,提升养老服务的智能化与精准化水平,助力智慧养老高质量发展。
|
11月前
|
Linux Shell
在Linux、CentOS7中设置shell脚本开机自启动服务
以上就是在CentOS 7中设置shell脚本开机自启动服务的全部步骤。希望这个指南能帮助你更好地管理你的Linux系统。
1395 25
|
网络协议 Java Shell
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
java spring 项目若依框架启动失败,启动不了服务提示端口8080占用escription: Web server failed to start. Port 8080 was already in use. Action: Identify and stop the process that’s listening on port 8080 or configure this application to listen on another port-优雅草卓伊凡解决方案
910 7