转载 :
Ubuntu18配置elasticsearch服务开机自启踩坑 :https://blog.csdn.net/qq_32635069/article/details/89003055?depth_1-utm_source=distribute.pc_relevant.none-task&utm_source=distribute.pc_relevant.none-task
1、查看系统类型
应该确定当前系统的初始化系统使用的那种类型
常见的用sysvinit、UpStart、systemd这三种
通过运行ps ax | grep init
如果出现了进程号为1的init进程,说明是前两者,然后看/etc/initlab文件是否存在,如果存在,则为sysvinit。否则为UpStart。
如果没有则 ps ax | grep systemd ,出现进程号为1的systemd进程,则为systemd初始化系统。
不同的初始化系统的自定义配置服务的方式不同
1.1在/etc/init.d目录下,添加执行的脚本,并指明RUNLEVEL以及abort等内容
避免类似下面的报错
update-rc.d: error: mydaemonDefault-Start contains no runlevels, aborting
给出elasticsearch的样例脚本
#!/bin/sh
#description: elasticsearch
### BEGIN INIT INFO
# Provides: elasticsearch
# Required-Start: $all
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop:
# Short-Description: start elasticsearch
### END INIT INFO
case "$1" in
start)
cd /home/fengyuluo/local/elasticsearch-6.3.2
./bin/elasticsearch -d
!
echo "elasticsearch startup"
;;
stop)
es_pid=`ps aux|grep elasticsearch | grep -v 'grep elasticsearch' | awk '{print $2}'`
kill -9 $es_pid
echo "elasticsearch stopped"
;;
restart)
es_pid=`ps aux|grep elasticsearch | grep -v 'grep elasticsearch' | awk '{print $2}'`
kill -9 $es_pid
echo "elasticsearch stopped"
cd /home/fengyuluo/local/elasticsearch-6.3.2
./bin/elasticsearch -d
!
echo "elasticsearch startup"
;;
*)
echo "start|stop|restart"
;;
esac
exit $?
1.2 去/lib/systemd/system目录下添加服务脚本
elasticsearch.serivce
[Unit]
Description=elasticsearch
After=network.target
[Service]
Type=simple
ExecStart=/bin/bash /home/fengyuluo/local/elasticsearch-6.3.2/bin/elasticsearch
Restart=always
User=fengyuluo
Group=fengyuluo
WorkingDirectory=/home/fengyuluo/local/elasticsearch-6.3.2
[Install]
WantedBy=mutil-user.target
指明user和group,普通用户,避免elasticsearch因为root用户运行而报错
1.3 执行sudo systemctl daemon-reload
1.4 重启服务
sudo systemctl elasticsearch.service
1.5 设置服务开机自启
sudo systemctl enable elasticsearch.service