ubuntu 20.04 | 设置开机启动脚本
1. 编辑 rc-local.service 文件
sudo chmod 777 /lib/systemd/system/rc-local.service
sudo gedit /lib/systemd/system/rc-local.service
\qquad 在 r c − l o c a l . s e r v i c e rc-local.service rc−local.service 文件尾部添加以下内容:
[Install]
WantedBy=multi-user.target
Alias=rc-local.service
2. 编辑 rc.local 文件
\qquad 编辑 r c . l o c a l rc.local rc.local 文件并添加要开机执行的脚本,第一行#!/bin/sh
,尾行exit 0
。( u b u n t u 18.04 ubuntu18.04 ubuntu18.04 版本之后默认没有 / e t c / r c . l o c a l /etc/rc.local /etc/rc.local 文件,需要自己创建)
sudo gedit /etc/rc.local
\qquad 添加下列内容:
#!/bin/sh
echo "看到这行字,说明添加自启动脚本成功。" > /usr/local/test.log
# 中间这一段就是脚本的内容,例如:sudo ssr start
exit 0
\qquad 给 r c . l o c a l rc.local rc.local 文件加上权限。
sudo chmod +x /etc/rc.local
3. 创建软链接
\qquad 在 / e t c / s y s t e m d / s y s t e m /etc/systemd/system /etc/systemd/system 目录下创建软链接。 u b u n t u ubuntu ubuntu 创建软链接类似于 w i n d o w s windows windows 下创建快捷方式。
# 创建方式:ln -s 原目录 映射目录
sudo ln -s /lib/systemd/system/rc-local.service /etc/systemd/system/