Ubuntu18设置rc.local开机自启动

/etc/systemd/system/rc-local.service文件中追加以下内容

1
2
[Install]
WantedBy=multi-user.target

创建文件sudo touch /etc/rc.local,然后将以下内容复制到文件中

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#!/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

给rc.local加上权限

1
sudo chmod +x /etc/rc.local

启用服务

1
sudo systemctl enable rc.local

启动服务并检查状态

1
2
sudo systemctl start rc.local.service
sudo systemctl status rc.local.service

重启并检查test.log文件中的内容,确认脚本是否启动成功

1
cat /usr/local/test.log