编辑/etc/rc.local文件
sudo vi /etc/rc.local
打开之后如果发现是新建的文件,也不要害怕,添加以下信息
1 | #!/bin/sh -e |
之后保存并退出
然后执行
1 | sudo chmod 755 /etc/rc.local |
若报错
提示:
1 | The unit files have no installation config (WantedBy, RequiredBy, Also, Alias settings in the [Install] section, and DefaultInstance for template units). This means they are not meant to be enabled using systemctl. |
就去设置rc-local.service
执行sudo vim /etc/systemd/system/rc-local.service
添加以下内容
1 | [Unit] |
再次执行
sudo systemctl enable rc-local.service
注意:
注意你要开机执行的语句,如果包含路径,最好使用决定路径
另一个办法
还有一个/etc/init.d/中创建自己的开机运行脚本的方法,可以参考Ubuntu/Linux 开机运行指定的命令/自动运行命令
本文参考: