0%

为debian添加rc.local开机自启服务

debian11及以上没有rc.local这个开机自启文件了,某些时候有些许不便,但是我们可以把它加回来!

写入rc.local文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
cat > /etc/rc.local << EOF
#!/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
EOF

可以在“exit 0”之上添加需要开机执行的命令。

授予执行权限

1
chmod +x /etc/rc.local

写入systemd文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
cat > /lib/systemd/system/rc-local.service << EOF
[Unit]
Description=/etc/rc.local Compatibility
Documentation=man:systemd-rc-local-generator(8)
ConditionFileIsExecutable=/etc/rc.local
After=network.target

[Service]
Type=forking
ExecStart=/etc/rc.local start
TimeoutSec=0
RemainAfterExit=yes
GuessMainPID=no
EOF

重载配置以及设置rc.local开机自启

1
2
3
systemctl daemon-reload
systemctl start rc-local.service
systemctl enable rc-local.service