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

这个错误信息表明程序尝试访问文件`/home/hadoop/soft/elasticsearch-5.4.3/config/elasticsearch.yml`时遇到了权限不足的问题Java抛出了`java.nio.file.AccessDeniedException`异常。 以下是可能导致此问题的原因及解决办法: ### 原因分析 1. **用户权限不足**:当前运行程序的用户对目标目录或文件没有足够的读、写或其他操作权限。 - 检查该路径下的所有父级目录是否都允许当前用户访问,并确认文件本身是否有正确的权限设置。 2. **文件系统限制**:如果目标文件位于只读挂载点上,则任何修改都会失败,包括创建新内容等动作。 - 确保所在的磁盘分区不是“只读”状态;可以使用命令 `mount | grep <partition>` 来查看挂载选项。 3. **安全机制干扰**: - 如果操作系统启用了SELinux或者AppArmor之类的安全模块,它们可能会进一步限制某些特定应用对资源的操作能力,即便常规意义上看起来应该有足够权限也可能无法完成任务。 - 可通过临时禁用SELinux(`setenforce 0`) 或调整策略规则来进行排查测试(生产环境需谨慎)。 ### 解决方案示例 #### 方法一: 修改文件权限 ```bash sudo chmod 644 /home/hadoop/soft/elasticsearch-5.4.3/config/elasticsearch.yml ``` 给定适当的权限使得普通进程能够正常打开和读取配置文件即可满足需求而不会造成安全隐患。 #### 方法二: 更改文件所属组或拥有者 将文件归属改为实际执行Elasticsearch服务的身份账户名hadoop为例: ```bash sudo chown hadoop:hadoop /home/hadoop/soft/elasticsearch-5.4.3/config/elasticsearch.yml ``` #### 方法三: 使用合适的启动身份 保证以具备上述条件的角色去开启软件实例,例如切换至对应非特权账号后再手动触发脚本流程而非root直接操控。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值