第一种:直接修改/etc/rc.d/rc.local文件
在/etc/rc.d/rc.local文件中需要输入两行,其中export JAVA_HOME=/usr/java/jdk1.8.0_112是必须要有的,否则开机启动不成功,大家根据自己JDK安装的位置自行更改。另一行/usr/local/zookeeper-3.4.5/bin/zkServer.sh start则是我们zookeeper的启动命令。配置好之后,重启虚拟机,会发现已经可以开机自启了。
[root@zookeeper ~]# vim /etc/rc.d/rc.local #!/bin/sh # # This script will be executed *after* all the other init scripts. # You can put your own initialization stuff in here if you don't # want to do the full Sys V style init stuff. touch /var/lock/subsys/local export JAVA_HOME=/usr/java/jdk1.8.0_112 /usr/local/zookeeper-3.4.5/bin/zkServer.sh start
第二种:把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 20 90 #description:zookeeper #processname:zookeeper export JAVA_HOME=//usr/java/jdk1.8.0_112 case $1 in start) su root /usr/local/zookeeper-3.4.5/bin/zkServer.sh start;; stop) su root /usr/local/zookeeper-3.4.5/bin/zkServer.sh stop;; status) su root /usr/local/zookeeper-3.4.5/bin/zkServer.sh status;; restart) su /usr/local/zookeeper-3.4.5/bin/zkServer.sh restart;; *) echo "require start|stop|status|restart" ;; 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.4.5/bin/../conf/zoo.cfg Starting zookeeper ... STARTED [root@zookeeper init.d]# service zookeeper status JMX enabled by default Using config: /usr/local/zookeeper-3.4.5/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.4.5/bin/../conf/zoo.cfg Stopping zookeeper ... STOPPED [root@zookeeper init.d]# service zookeeper status JMX enabled by default Using config: /usr/local/zookeeper-3.4.5/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服务了。
开机自启配置好了,我们重启一下试试,如下所示。一切正常!!说明我们的开机自启动成功了。
[root@zookeeper ~]# /usr/local/zookeeper-3.4.5/bin/zkServer.sh status JMX enabled by default Using config: /usr/local/zookeeper-3.4.5/bin/../conf/zoo.cfg Mode: standalone