Cenots Ubuntu linux系统服务脚本开机自启方法

Linux设置服务开机自动启动的方式有好多种,这里分别介绍一下centos操作系统开机脚本自启动和ubuntu操作系统脚本开机自启动的方式.

 

操作系统:Centos

这里通过chkconfig命令添加脚本为开机自动启动的方法。

 

1.编写脚本start-zookeeper.sh

(这里以开机启动zookeeper服务为例),脚本内容如下:

#!/bin/sh
#chkconfig: 2345 80 90
#description:开机自动启动的脚本程序

# 开启zookeeper服务
/opt/zookeeper/bin/zkServer.sh start

 

脚本第一行 “#!/bin/sh” 告诉系统使用的shell;
脚本第二行 “#chkconfig: 2345 80 90” 表示在2/3/4/5运行级别启动,启动序号(S80),关闭序号(K90);
脚本第三行 表示的是服务的描述信息

注意: 第二行和第三行必写,负责会出现如“服务 autostart.sh 不支持 chkconfig”这样的错误。

 

2. 将写好的start-zookeeper.sh脚本移动到/etc/rc.d/init.d/目录下

 

3. 给脚本赋可执行权限

 

cd /etc/rc.d/init.d/
chmod +x start-zookeeper.sh

 

4. 添加脚本到开机自动启动项目中

chkconfig --add start-zookeeper.sh
chkconfig start-zookeeper.sh on

 

到这里就设置完成了,我们只需要重启一下我们的服务器,就能看到我们配置的zookeeper服务已经可以开机自动启动了。

转自:https://blog.csdn.net/daidaineteasy/article/details/54864776

 

操作系统:Ubuntu

 

方法一: 编辑/etc/下的rc.local脚本(16.04无效)

然后把对应的需要执行的脚本写在exit 0前面,在ubuntu16.06上亲测无效

 

方法二:update-rc.d xxx

1.编写脚本

注:ubuntu 16.04中一定要加上以下LSB信息,不然放入启动脚本的时候会报错无法开机启动。

#!/bin/sh
### BEGIN INIT INFO
# Provides:          start-zookeeper.sh
# Required-start:    $local_fs $remote_fs $network $syslog
# Required-Stop:     $local_fs $remote_fs $network $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the start-zookeeper.sh daemon
# Description:       starts start-zookeeper.sh using start-stop-daemon
### END INIT INFO

/opt/zookeeper/bin/zkServer.sh start

 

2.复制或软连接脚本到/etc/init.d/目录下

 

3. 给脚本赋可执行权限

cd /etc/rc.d/init.d/
chmod +x start-zookeeper.sh

 

4.更新脚本的启动顺序

cd /etc/init.d
update-rc.d start-zookeeper.sh defaults 90

 

注:其中数字90是脚本启动的顺序号,按照自己的需要相应修改即可。在你有多个启动脚本,而它们之间又有先后启动的依赖关系时你就知道这个数字的具体作用了。(如hadoop 需要先启动zookeeper,则把hadoop的顺序号大于zookeeper启动的顺序号即可)

 

5.卸载启动脚本的方法

cd /etc/init.d
update-rc.d -f start-zookeeper.sh remove

 

转自:http://www.linuxdiyf.com/linux/26896.html

 

好了:以上就是centos和ubuntu系统的各自的脚本开机自启方法!(亲测有效!)

posted @ 2020-10-27 12:59  彬在俊  阅读(258)  评论(0编辑  收藏  举报