linux--ubuntu 16 自启动 rc.local

1 简介

在相应的启动脚本中按照规则加上对应的启动脚本语句,使用最多的启动脚本是 rc.local 文件。大家习惯在rc.local文件中配置启动信息(也可以在别的启动脚本中配置)。

2 基础知识

2.1 linux启动流程

(1)加载BIOS的硬件信息与进行自我测试, 并依据设置取得第一个可启动的设备;
(2)读取并执行第一个启动设备内MBR的boot loader(即grub、spfdisk等程序);
(3)一句boot loader 的设置加载Kernel,Kernel会开始检测硬件与加载驱动程序;
(4)在硬件驱动成功后,Kernel会主动调用init进程,而 init 会取得 run-level 信息;
(5)init 执行/etc/rc.d/rc.sysinit 文件来准备软件执行的操作环境(如网络时区等);
(6)init 执行 run-level 的各个服务器的启动(script方式);
(7)init 执行/etc/rc.d/rc.local 文件;
(8)init 执行终端机模拟程序 mingetty 来启动login 进程,最后就等待用户登陆。

3 自启动设置步骤

4 还原配置

5 定位问题

5.1 rc.local是否执行

  • rc.local文件添加日志输出功能,看是否有日志文件生成,若有,证明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.

#log
exec 2> /home/ubuntu/workspace/rc.local.log  # send stderr from rc.local to a log file  
exec 1>&2                  # send stdout to the same log file  
set -x                     # tell sh to display commands before execution 

#added by worthsen
myServer
#end

exit 0

5.2 rc.local是否执行失败

  • 终端输入该命令查看rc.local执行失败的原因
 systemctl status rc.local

5.3 重启rclocal命令

systemctl restart rc-local

可以通过这样来检测开机启动是否能成功,而不是每次都要重启,重启!

6 注意事项

6.1 权限

要加上对应的执行权限

6.2 绝对路径

尽量使用绝对路径,~对应的/home/ubuntu有时会被代替为/root,从而出错

6.3 bash 代替 sh

#!/bin/sh
对应bin中的dash,版本老,有些新命令不支持,从而报错
#!/bin/bash 推荐

6.4 rc.local中命令加权限

如下语句,如果脚本中需要sudo权限的操作会执行失败

sh /run/media/mmcblk0p4/Sen/test_start.sh &

正确如下,注意设置管理用户使用sudo命令免密模式

sudo  sh /run/media/mmcblk0p4/Sen/test_start.sh &

参考

1、理解Linux系统/etc/init.d目录和/etc/rc.local脚本
2、linux–rc.local
3、linux–切换ubuntu启动方式 及 还原配置
4、ubuntu 在 rc.local 里添加了命令为什么无法执行?
5、Ubuntu 16.04设置rc.local开机启动命令/脚本的方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

worthsen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值