
Ubuntu 16 以前,可以在 /etc/rc.local 文件内写上你要执行的命令或者脚本,开机后,系统就会以管理员权限去执行这些命令。但是在Ubuntu18 版本开始,Ubuntu使用了systemd 替代了 initd 管理系统。并且默认已经取消了 /etc/rc.local 文件。所以ubuntu-18.04不能像ubuntu16一样通过编辑rc.local来设置开机启动脚本,通过以下设置,可使rc.local重新发挥作用。
准备:如果没有安装nano,可以使用以下命令安装nano工具
sudo apt-get install nano
1.建立并编辑rc-local.servic文件
sudo nano /etc/systemd/system/rc-local.service
2. 将下列内容复制进rclocal.service文件保存
[Unit]
Description=/etc/rc.local Compatibility 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
3. 创建文rc.local
sudo nano /etc/rc.local
4. 将下列内容复制进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. # start echo "看到这行字,说明添加自启动脚本成功。" > /usr/local/test.log # end # start与end之前的内容,即开机启动后会自动执行的命令,可以执行Linux指令、shell脚本、启动web服务等 exit 0
5. 给rc.local加上执行权限
sudo chmod +x /etc/rc.local
6. 启用服务
sudo systemctl enable rc-local
7. 启动服务并检查状态
sudo systemctl start rc-local.service sudo systemctl status rc-local.service
8.重启并检查test.log文件
cat /usr/local/test.log
9.开启启动服务配置成功