Linux程序开机自启动(以zookeeper为例)
在/etc/rc.d/init.d 目录下创建脚本, 例如 zookeeper.sh, 内容如下
#!/bin/bash #chkconfig: 2345 10 90 #description: service zookeeper #process name:zookeeper #环境变量加载 source /etc/profile export JAVA_HOME=/opt/module/jdk1.8.0_341 export ZOOKEEPER_HOME=/opt/module/zookeeper-3.5.7 case "$1" in start) su root ${ZOOKEEPER_HOME}/bin/zkServer.sh start;; 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;; *) echo "require start|start-foreground|stop|status|restart|print-cmd";; esac
添加到开机自启动服务(关键)
# 上面相当于创建了一个zookeeper.sh脚本, 添加到开机自启动服务相当于调用zookeeper.sh start命令 chkconfig --add zookeeper
查看开机自启动服务
chkconfig --list
使用
systemctl start zookeeper
命令来启动服务, 或者使用systemctl status zookeeper
命令来查看服务运行状态