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

简介: 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
  1. 启动停止一起测试
# 启动
[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月前
|
存储 安全 Unix
七、Linux Shell 与脚本基础
别再一遍遍地敲重复的命令了,把它们写进Shell脚本,就能一键搞定。脚本本质上就是个存着一堆命令的文本文件,但要让它“活”起来,有几个关键点:文件开头最好用#!/usr/bin/env bash来指定解释器,并用chmod +x给它执行权限。执行时也有讲究:./script.sh是在一个新“房间”(子Shell)里跑,不影响你;而source script.sh是在当前“房间”里跑,适合用来加载环境变量和配置文件。
613 10
|
7月前
|
存储 Shell Linux
八、Linux Shell 脚本:变量与字符串
Shell脚本里的变量就像一个个贴着标签的“箱子”。装东西(赋值)时,=两边千万不能有空格。用单引号''装进去的东西会原封不动,用双引号""则会让里面的$变量先“变身”再装箱。默认箱子只能在当前“房间”(Shell进程)用,想让隔壁房间(子进程)也能看到,就得给箱子盖个export的“出口”戳。此外,Shell还自带了$?(上条命令的成绩单)和$1(别人递进来的第一个包裹)等许多特殊箱子,非常有用。
629 2
|
10月前
|
Shell
Shell脚本循环控制:shift、continue、break、exit指令
使用这些命令可以让你的Shell脚本像有生命一样动起来。正确使用它们,你的脚本就能像一场精心编排的舞蹈剧目,既有旋律的起伏,也有节奏的跳跃,最终以一场惊艳的表演结束。每一个动作、每一个转折点,都准确、优雅地完成所需要表达的逻辑。如此,你的脚本不只是冰冷的代码,它透过终端的界面,跳着有节奏的舞蹈,走进观众——使用者的心中。
349 60
|
7月前
|
数据采集 监控 Shell
无需Python:Shell脚本如何成为你的自动化爬虫引擎?
Shell脚本利用curl/wget发起请求,结合文本处理工具构建轻量级爬虫,支持并行加速、定时任务、增量抓取及分布式部署。通过随机UA、异常重试等优化提升稳定性,适用于日志监控、价格追踪等场景。相比Python,具备启动快、资源占用低的优势,适合嵌入式或老旧服务器环境,复杂任务可结合Python实现混合编程。
|
9月前
|
Web App开发 缓存 安全
Linux一键清理系统垃圾:释放30GB空间的Shell脚本实战​
这篇博客介绍了一个实用的Linux系统盘清理脚本,主要功能包括: 安全权限检查和旧内核清理,保留当前使用内核 7天以上日志文件清理和系统日志压缩 浏览器缓存(Chrome/Firefox)、APT缓存、临时文件清理 智能清理Snap旧版本和Docker无用数据 提供磁盘空间使用前后对比和大文件查找功能 脚本采用交互式设计确保安全性,适合定期维护开发环境、服务器和个人电脑。文章详细解析了脚本的关键功能代码,并给出了使用建议。完整脚本已开源,用户可根据需求自定义调整清理策略。
1060 1
|
11月前
|
存储 Unix Shell
确定Shell脚本在操作系统中的具体位置方法。
这对于掌握Linux的文件系统组织结构和路径方面的理解很有帮助,是我们日常工作和学习中都可能使用到的知识。以上讲解详细清晰,应用简便,是每一个想要精通操作系统的计算机爱好者必备的实用技能。
444 17
|
12月前
|
Linux Shell
在Linux、CentOS7中设置shell脚本开机自启动服务
以上就是在CentOS 7中设置shell脚本开机自启动服务的全部步骤。希望这个指南能帮助你更好地管理你的Linux系统。
1521 25
|
11月前
|
Linux Shell
Centos或Linux编写一键式Shell脚本删除用户、组指导手册
Centos或Linux编写一键式Shell脚本删除用户、组指导手册
320 4
|
11月前
|
Linux Shell 数据安全/隐私保护
Centos或Linux编写一键式Shell脚本创建用户、组、目录分配权限指导手册
Centos或Linux编写一键式Shell脚本创建用户、组、目录分配权限指导手册
569 3
|
10月前
|
文字识别 监控 Java
顺丰同城抢单辅助脚本,顺丰骑士抢单辅助免封号,自动抢单神器【java版开源】
这是一套OCR实时检测订单列表并自动右滑的完整代码方案,适用于学习研究。代码包含四个主要模块:OCR处理(文字识别)、价格分析