方法一:
1.创建脚本文件(以.sh结尾)
# vi elasticsearch.sh
#!/bin/bash
# kconfig: 2345 91 11
# description: elasticsearch
#定义变量
PORT=9200
NAME=elasticsearch
ID=`ps -aux | grep "$NAME" | grep -v "grep" | awk '{print $2}'`
CHECK_PORT=`netstat -tnlp|grep "$PORT"`
ELASTICSEARCH_SERVER=/usr/software/elasticsearch-6.5.4/bin/elasticsearch
RETAVL=0
#检查shelk公共函数库是否存在,存在就加载
FUNCTIONS_PATH=/etc/init.d/functions
[ -f $FUNCTIONS_PATH ]&& source $FUNCTIONS_PATH
#检查文件是否存在并可执行
[ -x $ELASTICSEARCH_SERVER ]|| exit 0
#定义函数
#检查是否执行成功
check(){
RETAVL=$?
if
[[ "$RETAVL" = 0 ]];then
action "elasticsearch is $1" /bin/true
else
action "elasticsearch is $1" /bin/false
fi
}
#启动服务
start(){
if
[[ "$CHECK_PORT" != "" ]];then
echo "elasticsearch already running"
else
su - es <<!
$ELASTICSEARCH_SERVER -d
exit
!
check Started
fi
}
#停止服务
stop(){
if
[[ "$CHECK_PORT" = "" ]];then
echo "elasticsearch is not running."
else
kill -9 $ID
check Stopped
fi
}
#重启服务
restart(){
stop
sleep 2
start
}
#判断
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
*)
echo $"Usage:$0{start|stop|restart|help}"
esac
exit $RETAVL
2.增加脚本可执行权限
# chmod +x elasticsearch.sh
3.将脚本移动到/etc/rc.d/init.d目录下
# mv elasticsearch.sh /etc/rc.d/init.d
4.将脚本添加进开机启动
# cd /etc/rc.d/init.d
# chkconfig –add nginx.sh
# chkconfig nginx.sh on
5.查看开机启动列表
# chkconfig --list
方法二: