Ubuntu18设置rc.local开机自启动

目录
Ubuntu 18.04 使用 systemd 取代了传统的 init,rc.local 默认不再生效。本文介绍如何通过创建 systemd 服务单元来恢复 rc.local 开机自启动功能。
1 步骤一
在/etc/systemd/system/rc-local.service文件中追加以下内容
[Install]
WantedBy=multi-user.target
2 步骤二
创建文件sudo touch /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.
echo "看到这行字,说明添加自启动脚本成功。" > /usr/local/test.log
exit 0
3 步骤三
给rc.local加上权限
sudo chmod +x /etc/rc.local
4 步骤四
启用服务
sudo systemctl enable rc.local
5 步骤五
启动服务并检查状态
sudo systemctl start rc.local.service
sudo systemctl status rc.local.service
6 步骤六
重启并检查test.log文件中的内容,确认脚本是否启动成功
cat /usr/local/test.log