Debian 9 开机自动运行脚本

问题背景:想要在debian9系统中,自动执行系统reboot测试。
最简单的方式是把reboot测试脚本添加到/etc/rc.local中(exit 0)之前
Debian9 默认没有/etc/rc.local文件,但是可以通过systemd恢复。
首先,我们需要修改rc.local.service文件

#vim  /lib/systemd/system/rc-local.service

加入以下内容:
[Unit]
Description=/etc/rc.local
ConditionPathExists=/etc/rc.local
 
[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes
SysVStartPriority=99
 
[Install]
WantedBy=multi-user.target

其次,新建/etc/rc.loca文件,加入以下内容:
# vim /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

然后,修改rc.local 权限
# chmod +x /etc/rc.local

之后,就可以启动rc-local了
#systemctl start rc-local

查看rc-local启动过程中是否有错误出现
#systemctl status rc-local

到这里,rc-local就能够正常工作了。

根据需要,将开机自动reboot测试脚本放在/etc/rc.local 中exit 0 之前,就可以开机自动执行了。

# vim /etc/rc.local  
#!/bin/sh -e

……

/sbin/reboot

exit 0

保存退出之后,reboot系统重启即可。
 

你可能感兴趣的:(Debian 9 开机自动运行脚本)