把zookeeper做成服务
1、进入到/etc/rc.d/init.d目录下,新建一个zookeeper脚本
[root@zookeeper ~]# cd /etc/rc.d/init.d/
[root@zookeeper init.d]# pwd
/etc/rc.d/init.d
[root@zookeeper init.d]# touch zookeeper
2、给脚本添加执行权限
[root@zookeeper init.d]# chmod +x zookeeper
3、使用命令vim zookeeper进行编辑,在脚本中输入如下内容,其中同上面注意事项一样要添加export JAVA_HOME=/usr/java/jdk1.8.0_112这一行,否则无法正常启动。
[root@zookeeper init.d]# vim zookeeper
#!/bin/bash
#chkconfig:2345 10 90
#description:service zookeeper
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.181-3.b13.el7_5.x86_64
export ZOO_LOG_DIR=/data/zookeeper/logs
ZOOKEEPER_HOME=/data/zookeeper-3.6.3/
case "$1" in
start) su root ${ZOOKEEPER_HOME}/bin/zkServer.sh start;;
start-foreground) su root ${ZOOKEEPER_HOME}/bin/zkServer.sh start-foreground;;
stop) su root ${ZOOKEEPER_HOME}/bin/zkServer.sh stop;;
status) su root ${ZOOKEEPER_HOME}/bin/zkServer.sh status;;
restart) su root ${ZOOKEEPER_HOME}/bin/zkServer.sh restart;;
upgrade)su root ${ZOOKEEPER_HOME}/bin/zkServer.sh upgrade;;
print-cmd)su root ${ZOOKEEPER_HOME}/bin/zkServer.sh print-cmd;;
*) echo "requirestart|start-foreground|stop|status|restart|print-cmd";;
esac
4、使用service zookeeper start/stop命令来尝试启动关闭zookeeper,使用service zookeeper status查看zookeeper状态。
先来看启动及状态
[root@zookeeper init.d]# service zookeeper start
JMX enabled by default
Using config: /usr/local/zookeeper-3.6.3/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@zookeeper init.d]# service zookeeper status
JMX enabled by default
Using config: /usr/local/zookeeper-3.6.3/bin/../conf/zoo.cfg
Mode: standalone
[root@zookeeper init.d]#
接着看关闭及状态
[root@zookeeper init.d]# service zookeeper stop
JMX enabled by default
Using config: /usr/local/zookeeper-3.6.3/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
[root@zookeeper init.d]# service zookeeper status
JMX enabled by default
Using config: /usr/local/zookeeper-3.6.3/bin/../conf/zoo.cfg
Error contacting service. It is probably not running.
[root@zookeeper init.d]#
5、添加到开机自启
[root@zookeeper init.d]# chkconfig --add zookeeper
添加完之后,我们使用chkconfig --list来查看开机自启的服务中是否已经有我们的zookeeper了,如下所示,可以看到在最后一行便是我们的zookeeper服务了。
netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关
network 0:关 1:关 2:开 3:开 4:开 5:开 6:关
zookeeper 0:关 1:关 2:开 3:开 4:开 5:开 6:关