Centos7开机启动自定义脚本

linux开机自启动的方式跟window相似,也是把启动程序放在一个公共位置,随着开机自启动。

在CentOS 之前版本 ,把启动脚本放在/etc/rc.d/rc.local目录即可。但CentOS 7,/etc/rc.d/rc.local文件的权限被降低了,开机的时候执行自己的脚本是不能起动一些服务的。
查看下该文件的权限,就会知道,该文件没有执行权限。

[root@localhost ~]# cd /etc/rc.d/
[root@localhost rc.d]# ll
总用量 4
drwxr-xr-x. 2 root root  70 2月  27 23:30 init.d
drwxr-xr-x. 2 root root  45 2月  27 23:30 rc0.d
drwxr-xr-x. 2 root root  45 2月  27 23:30 rc1.d
drwxr-xr-x. 2 root root  45 2月  27 23:30 rc2.d
drwxr-xr-x. 2 root root  45 2月  27 23:30 rc3.d
drwxr-xr-x. 2 root root  45 2月  27 23:30 rc4.d
drwxr-xr-x. 2 root root  45 2月  27 23:30 rc5.d
drwxr-xr-x. 2 root root  45 2月  27 23:30 rc6.d
-rw-r--r--. 1 root root 473 8月   5 2017 rc.local

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13

而且查看rc.local文件,里面的注释也挺人性化,告诉你要一定要执行chmod +x /etc/rc.d/rc.local

[root@localhost rc.d]# cat rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local
[root@localhost rc.d]#

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16

实验

比如说:开机自动启动nginx服务(最近刚在虚拟机安装了一个nginx,拿来小试牛刀下)

先检查我当前nginx是未启动的

[root@localhost ~]# ps -ef|grep nginx
root       1238   1126  0 09:44 pts/0    00:00:00 grep --color=auto nginx
[root@localhost ~]#

    1
    2
    3
    4

一、新建执行脚本hbk.sh

脚本的位置在/root/hbk目录下

[root@localhost hbk]# pwd
/root/hbk
[root@localhost hbk]# ll
总用量 35820
-rw-r--r--.  1 root root        40 4月  13 09:47 hbk.sh
drwxr-xr-x.  3 root root        53 4月  12 17:33 nginx
drwxr-xr-x. 11 root root       151 4月  13 08:51 nginx2
drwxr-xr-x. 10  502 games     4096 11月  2 02:52 zookeeper-3.4.11
-rw-r--r--.  1 root root  36668066 11月  9 02:24 zookeeper-3.4.11.tar.gz
[root@localhost hbk]# cat hbk.sh
#!/bin/bash
/root/hbk/nginx2/sbin/nginx
[root@localhost hbk]#

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13

可以从上面看到hbk.sh没有执行权限。
二、使自启动程序拥有执行权限

[root@localhost hbk]# chmod +x hbk.sh
[root@localhost hbk]# ll
总用量 35820
-rwxr-xr-x.  1 root root        40 4月  13 09:47 hbk.sh
drwxr-xr-x.  3 root root        53 4月  12 17:33 nginx
drwxr-xr-x. 11 root root       151 4月  13 08:51 nginx2
drwxr-xr-x. 10  502 games     4096 11月  2 02:52 zookeeper-3.4.11
-rw-r--r--.  1 root root  36668066 11月  9 02:24 zookeeper-3.4.11.tar.gz
[root@localhost hbk]#

    1
    2
    3
    4
    5
    6
    7
    8
    9

三、在/etc/rc.d/rc.local中加入执行脚本命令,并设置执行权限。

在/etc/rc.d/rc.local文件末尾追加/root/hbk/hbk.sh

[root@localhost hbk]# vi /etc/rc.d/rc.local
[root@localhost hbk]# chmod +x /etc/rc.d/rc.local
[root@localhost hbk]# ll /etc/rc.d/rc.local
-rwxr-xr-x. 1 root root 492 4月  13 09:55 /etc/rc.d/rc.local
[root@localhost hbk]#

    1
    2
    3
    4
    5

简单的三步操作之后,重启下linux系统,验证下nginx服务已经开机启动了。
---------------------
作者:黄宝康
来源:CSDN
原文:https://blog.csdn.net/huangbaokang/article/details/79924058
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值