/etc/rc.local 和/etc/init.d/rc.local的区别

本文详细解析了Linux系统中rc.local脚本的工作原理及其执行流程。通过分析/etc/rc.local和/etc/init.d/rc.local两个文件的具体内容,展示了如何通过rc.local进行自定义启动脚本设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

从下面的命令注释内容可以看出,/etc/init.d/rc.local里面会判断是否存在/etc/rc.local,如果存在,就执行这个文件

 

 

/etc/rc.local的内容如下: cat /etc/rc.local 

 

 

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

exit 0

 

 

 

/etc/init.d/rc.local的内容如下: cat /etc/init.d/rc.local 

 

 

#! /bin/sh
### BEGIN INIT INFO
# Provides:          rc.local
# Required-Start:    $all
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:
# Short-Description: Run /etc/rc.local if it exist
### END INIT INFO


PATH=/sbin:/usr/sbin:/bin:/usr/bin

. /lib/init/vars.sh
. /lib/lsb/init-functions

do_start() {
	if [ -x /etc/rc.local ]; then
	        [ "$VERBOSE" != no ] && log_begin_msg "Running local boot scripts (/etc/rc.local)"
		/etc/rc.local
		ES=$?
		[ "$VERBOSE" != no ] && log_end_msg $ES
		return $ES
	fi
}

case "$1" in
    start)
	do_start
        ;;
    restart|reload|force-reload)
        echo "Error: argument '$1' not supported" >&2
        exit 3
        ;;
    stop)
        ;;
    *)
        echo "Usage: $0 start|stop" >&2
        exit 3
        ;;
esac

 

 

<audio controls="controls" style="display: none;"></audio>

### 配置使用 Linux 系统中的 /etc/rc.d/rc.local 文件 #### 1. /etc/rc.d/rc.local 的用途 /etc/rc.d/rc.local 文件是一个脚本文件,用于在系统启动时执行用户自定义的命令或脚本[^2]。该文件通常在所有其他初始化脚本执行完毕后运行,因此可以用来启动用户特定的服务或执行额外的配置任务。 #### 2. 配置方法 为了确保 /etc/rc.d/rc.local 文件能够正常工作,需要进行以下步骤: - **编辑 rc.local 文件** 使用文本编辑器打开 /etc/rc.d/rc.local 文件,并添加希望在系统启动时执行的命令或脚本。例如: ```bash #!/bin/sh # This script will be executed *after* all the other init scripts. # You can put your own initialization stuff in here if you don't # want to do the full SysV style init stuff. # Print the date to a log file echo "System started at $(date)" >> /var/log/rc.local.log # Start a custom service or script /path/to/your/custom/script.sh exit 0 ``` 上述代码中,第一行 `#!/bin/sh` 是声明脚本解释器的必要部分,最后一行 `exit 0` 表示脚本成功执行[^2]。 - **设置执行权限** 确保 /etc/rc.d/rc.local 文件具有可执行权限: ```bash sudo chmod +x /etc/rc.d/rc.local ``` - **启用 rc-local 服务(适用于 systemd 系统)** 在使用 systemd 的现代 Linux 发行版中,需要启用 rc-local 服务以确保 /etc/rc.d/rc.local 文件在系统启动时被执行: ```bash sudo systemctl enable rc-local sudo systemctl start rc-local ``` #### 3. 验证配置是否生效 重新启动系统并检查 /etc/rc.d/rc.local 中的命令是否正确执行。例如,如果日志记录到 `/var/log/rc.local.log`,可以通过以下命令查看日志文件: ```bash cat /var/log/rc.local.log ``` #### 4. 注意事项 - 如果系统使用的是 systemd,则需要确保 rc-local 服务已正确配置并启用[^2]。 - /etc/rc.d/rc.local 文件的内容应以 `#!/bin/sh` 开头,并以 `exit 0` 结束,以保证脚本的正常运行[^3]。 #### 示例代码 以下是一个完整的 /etc/rc.d/rc.local 配置示例: ```bash #!/bin/sh # This script will be executed *after* all the other init scripts. # Log the system startup time echo "System started at $(date)" >> /var/log/rc.local.log # Start a custom service /path/to/your/custom/service.sh # Ensure the script exits successfully exit 0 ``` ###
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值