centos7下/etc/rc.local不生效的问题

centos7.x的系统/etc/rc.local和centos6.x有了变化。

/etc/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 constrast 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.

翻译(机翻): 此文件是为兼容性目的而添加的

建议使用自己的系统服务或udev规则来在引导期间运行脚本,而不是使用此文件。

由于引导期间并行执行,与之前的版本相比,此脚本将不会在所有其他服务之后运行。

请注意,您必须运行’chmod +x /etc/rc.d/rc.local’,以确保该脚本在引导过程中执行。

官方建议使用系统服务来实现服务的自启动。而不通过/etc/rc.local来实现自启动。在测试的工程中大致确认到了一些内容。
1.目前/etc/rc.local是通过rc-local.service来运行的。
2./etc/rc.local是一个脚本,开头必须有#!/bin/bash。
3.使用rc-local.service可能会获取不要一些系统的变量(在启动rabbitmq的时候,发现使用rc-local.service服务无法获取到$HOME变量,必须在/etc/rc.local重新声明下。)

vim /etc/rc.local
# 配置HOME变量
declare -x HOME="/root"
# 将erlang的命令增加到环境变量中。也可以在系统文件中设置.
declare -x PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/home/erlang/bin"

以下是如何让/etc/rc.local生效:
1.给/etc/rc.local执行权限
2./etc/rc.local要以#!/bin/bash开头
3.设置rc-local.service开机自启动
systemctl enable rc-local.service
4.检测rc-local.servce是否可以启动
systemctl start rc-local.service
5.执行了上面的命令以后,要查看下rc-local.service的状态,查看下启动信息以及确认是否有报错。
systemctl status rc-local.service
5.1 有报错提示

Job for rc-local.service failed because the control process exited with error code. See "systemctl status rc-local.service" and "journalctl -xe" for details.

5.2 不会有报错提示
5.3 不论有没有报错提示,都需要systemctl status rc-local.service查看下状态是否有报错。

Previous Post

centos7配置应用程序为系统服务

Next Post

centos7如何关闭ipv6

Related Posts