习惯了使用rc.local设置开机自启,不过Debian 9/Ubuntu 17+默认已经没有rc.local文件了,我们需要手动自己编辑。
1. 新增rc-local.service文件:
vi /etc/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
2. 新增rc-local文件:
vi /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
3. 添加权限并设置开机自启并启动脚本:
chmod +x /etc/rc.local
systemctl enable rc-local
systemctl start rc-local.service
4. 检查服务状态:
systemctl status rc-local.service
返回Active:active信息,表示成功了,然后就可以在/etc/rc.loacl中的exit前增加任意你想启动的脚本了。