将软件做成服务,并开机自启

将软件做成服务,可以让我们操作简便。如下面的zk

1、进入到/etc/rc.d/init.d目录下,新建一个zookeeper脚本

 

[[email protected] ~]# cd /etc/rc.d/init.d/
[[email protected]-777xz init.d]# pwd
/etc/rc.d/init.d
[[email protected]-777xz init.d]# touch zookeeper

 

2、给脚本添加执行权限

 

[[email protected] init.d]# chmod +x zookeeper

 

3、使用命令vim zookeeper进行编辑,在脚本中输入如下内容,其中同上面注意事项一定要添加JAVA_HOME=/usr/local/jdk/jdk1.8.0_241这一行,否则无法正常启动。
[[email protected]777xz init.d]# vim zookeeper 
 
#!/bin/bash
#chkconfig:2345 20 90
#description:zookeeper
#processname:zookeeper
export JAVA_HOME=/usr/local/jdk/jdk1.8.0_241
case $1 in
        start) su root /usr/local/zookeeper/apache-zookeeper-3.5.7-bin/bin/zkServer.sh start;;
        stop) su root /usr/local/zookeeper/apache-zookeeper-3.5.7-bin/bin/zkServer.sh stop;;
        status) su root /usr/local/zookeeper/apache-zookeeper-3.5.7-bin/bin/zkServer.sh status;;
        restart) su /usr/local/zookeeper/apache-zookeeper-3.5.7-bin/bin/zkServer.sh restart;;
        *) echo "require start|stop|status|restart" ;;
esac

4、使用service zookeeper start/stop命令来尝试启动关闭zookeeper,使用service zookeeper status查看zookeeper状态。
先来看启动及状态

[[email protected] /]# service zookeeper start
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/apache-zookeeper-3.5.7-bin/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[[email protected]-777xz /]# service zookeeper status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/apache-zookeeper-3.5.7-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: standalone

接着看关闭及状态

[[email protected] /]# service zookeeper stop
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/apache-zookeeper-3.5.7-bin/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
[[email protected]-777xz /]# service zookeeper status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/apache-zookeeper-3.5.7-bin/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Error contacting service. It is probably not running.

5、添加到开机自启

[[email protected] /]# chkconfig --add zookeeper
添加完之后,我们使用chkconfig --list来查看开机自启的服务中是否已经有我们的zookeeper了,如下所示,可以看到在最后一行便是我们的zookeeper服务了。
[[email protected]-777xz /]# chkconfig --list
netconsole         0:关    1:关    2:关    3:关    4:关    5:关    6:关
network            0:关    1:关    2:开    3:开    4:开    5:开    6:关
redis              0:关    1:关    2:开    3:开    4:开    5:开    6:关
zookeeper          0:关    1:关    2:开    3:开    4:开    5:开    6:关

ok,reboot重启后看下服务是否启动吧。

猜你喜欢

转载自www.cnblogs.com/two-bees/p/12581408.html