1.在/etc/init.d目录下新建一个elasticsearch脚本内容如下
#!/bin/bash
source /etc/profile
case "$1" in
start)
su es<<!
cd /home/es/app/elasticsearch-7.10.0/bin
./elasticsearch -d
!
echo "start elasticsearch"
;;
stop)
es_pid=ps aux|grep elasticsearch | grep -v 'grep elasticsearch' | awk '{print $2}'
kill -9 $es_pid
echo "stop elasticsearch"
;;
restart)
es_pid=ps aux|grep elasticsearch | grep -v 'grep elasticsearch' | awk '{print $2}'
kill -9 $es_pid
echo "stop elasticsearch"
su es<<!
cd /home/es/app/elasticsearch-7.10.0/bin
./elasticsearch -d
!
echo "start elasticsearch"
;;
*)
echo "start|stop|restart"
;;
esac
exit $?
2.添加服务chkconfig --add 脚本
3.添加开机启动 chkconfig 脚本 on