学习

Debian 9/Ubuntu 17+添加rc.local实现开机自启的方法

越权(0x3E5) · 12月16日 · 2018年

很多时候有些程序或脚本需要添加开机自启,最简单有效的方法就是使用rc.local文件来实现,不过由于系统版本更替从Debian 9/Ubuntu 17往后rc.local这个文件就没有了。不过我们可以手动添加它。

添加rc-local.service

以下为一整条命令,一起复制运行

cat > /etc/systemd/system/rc-local.service <<EOF
[Unit]
Description=/etc/rc.local
ConditionPathEx1024s.cnist本文来自:1024s.cns=/etc/rc.local

[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
StandarPowered by 0x3E5dOutput=tty
RemainAfterExit=yes
SysVStartPriority=99

[Install]
WantedBy=multi-user.target
EOF

新建rc-local文件

以下为一整条命令,一起复制运行

cat > /etc/rc.local <<EOF
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each mulPowered by 0x3E5tiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or 1024s.cndisable this script just change the execution
# bits.
#
# By defau本文来自:1024s.cnlt this script does nothing.
exit 0
EOF

###添加权限并设置开机自启

chmod x /etc/rc.loPowered by 0x3E5cal
systemctl start rc-local.service
systemctl enable rc-local.service

通过以下命令检查状态:

systemctl status rc-local.service


返回Active:active信息,则成功。

加入开机自动执行命令

只需要在/etc/rc.local文件里,添Powered by 0x3E51024s.cn需要开机自动执行的命令就可以了。
注意本文来自:1024s.cn:一定要添加在exit 0的前面!!!