写个脚本

启动zookeeper 为例

脚本

  1 
  2 #!/bin/bash
  3 #description:开机自启脚本
  4 # 开机启动zookeeper
  5 /usr/local/services/zookeeper/zookeeper-3.4.14/bin/zkServer.sh start

设置运行权限&执行,可以看到zookeeper已经运行,说明脚本正常

[root@localhost myScrip]# chmod -R 777 zookeeper-scrip-start.sh    

[root@localhost myScrip]# ./zookeeper-scrip-start.sh 
ZooKeeper JMX enabled by default
Using config: /usr/local/services/zookeeper/zookeeper-3.4.14/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@localhost myScrip]# /usr/local/services/zookeeper/zookeeper-3.4.14/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/services/zookeeper/zookeeper-3.4.14/bin/../conf/zoo.cfg
Mode: standalone

添加开启启动

进入/etc/rc.d/ 修改rc.local 添加脚本执行的绝对路径/home/myScrip/zookeeper-scrip-start.sh


[root@localhost myScrip]# cd /etc/rc.d/
[root@localhost rc.d]# ls
init.d  rc0.d  rc1.d  rc2.d  rc3.d  rc4.d  rc5.d  rc6.d  rc.local
[root@localhost rc.d]# vim rc.local 
  1 #!/bin/bash
  2 # THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
  3 #
  4 # It is highly advisable to create own systemd services or udev rules
  5 # to run scripts during boot instead of using this file.
  6 #
  7 # In contrast to previous versions due to parallel execution during boot
  8 # this script will NOT be run after all other services.
  9 #
 10 # Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
 11 # that this script will be executed during boot.
 12 
 13 touch /var/lock/subsys/local
 14 
 15 /home/myScrip/zookeeper-scrip-start.sh

重启系统 reboot

发现没有成功,发现是rc.local权限不够
添加权限 chmod +x /etc/rc.d/rc.local
重启后查看到zookeeper已经运行

[root@localhost rc.d]# chmod +x /etc/rc.d/rc.local
[root@localhost rc.d]# ll
total 4
drwxr-xr-x. 2 root root  70 Apr 25 02:43 init.d
drwxr-xr-x. 2 root root  45 Apr  3 04:42 rc0.d
drwxr-xr-x. 2 root root  45 Apr  3 04:42 rc1.d
drwxr-xr-x. 2 root root  45 Apr  3 04:42 rc2.d
drwxr-xr-x. 2 root root  45 Apr  3 04:42 rc3.d
drwxr-xr-x. 2 root root  45 Apr  3 04:42 rc4.d
drwxr-xr-x. 2 root root  45 Apr  2 20:53 rc5.d
drwxr-xr-x. 2 root root  45 Apr  3 04:42 rc6.d
-rwxr-xr-x. 1 root root 513 May 16 20:55 rc.local
[root@localhost rc.d]# ls
init.d  rc0.d  rc1.d  rc2.d  rc3.d  rc4.d  rc5.d  rc6.d  rc.local
[root@localhost rc.d]# reboot 
Last login: Thu May 16 20:56:09 2019 from 192.168.177.1
[root@localhost ~]# ls
anaconda-ks.cfg  Desktop  Documents  Downloads  initial-setup-ks.cfg  Music  original-ks.cfg  Pictures  Public  Templates  Videos  zookeeper.out
[root@localhost ~]# /etc/
Display all 149 possibilities? (y or n)
[root@localhost ~]# /usr/local/services/
kafka/     zookeeper/ 
[root@localhost ~]# /usr/local/services/
kafka/     zookeeper/ 
[root@localhost ~]# /usr/local/services/zookeeper/zookeeper-3.4.14/bin/zk
zkCleanup.sh         zkCli.sh             zkEnv.sh             zkServer.sh          zkTxnLogToolkit.sh   
zkCli.cmd            zkEnv.cmd            zkServer.cmd         zkTxnLogToolkit.cmd  
[root@localhost ~]# /usr/local/services/zookeeper/zookeeper-3.4.14/bin/zkServer.sh status 
ZooKeeper JMX enabled by default
Using config: /usr/local/services/zookeeper/zookeeper-3.4.14/bin/../conf/zoo.cfg

reference
【centos7】添加开机启动服务/脚本

最后修改:2020 年 02 月 13 日
如果觉得我的文章对你有用,请随意赞赏