美文网首页
ubuntu18.04设置每次开机自启动后执行临时挂载命令

ubuntu18.04设置每次开机自启动后执行临时挂载命令

作者: silas简 | 来源:发表于2023-06-25 09:52 被阅读0次

ubuntu-18.04不能像ubuntu14一样通过编辑rc.local来设置开机启动脚本,通过下列简单设置后,可以使rc.local重新发挥作用。

1.建立rc-local.service文件

sudo vim /etc/systemd/system/rc-local.service

2.将下列内容复制进rc-local.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 vim /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.
echo "看到这行字,说明添加自启动脚本成功。" > /usr/local/test.log
exit 0

5.给rc.local加上权限,启用服务
加上权限

sudo chmod +x /etc/rc.local

启用服务

sudo systemctl enable rc-local

6.启动服务并检查状态
启动服务

sudo systemctl start rc-local.service

检查状态

sudo systemctl status rc-local.service

7.重启并检查test.log文件

cat /usr/local/test.log

如果以上成功了,那么只需稍微改一下。

注意:我使用的方法是将/etc/rc.local的文件当做一个启动索引文件来使用,就是把它当做一个专门用来启动其他.sh文件来使用的,你也可以直接用/etc/rc.local来启动你想要启动的程序或者脚本。
1.创建你自己需要的 .sh文件

sudo vim test.sh

文件名:test.sh
shell脚本

#!\bin\bash
cd /home/lbw/
python ce.py
exit 0

然后加执行权限

sudo chmod +x test.sh

如图:


image.png

2.写上你需要执行的内容
例如:前提是你写好了py文件

sudo vim ce.py

文件名:ce.py
python测试小程序

with open("sb.txt", "w") as f:
    f.write("SB")

3.修改一下刚才测试用的文件

sudo vim /etc/rc.local
image.png

启动 test.sh 这个文件,当然,你可以随便定义名字与位置
注意:如果不成功,那么可以使用

sudo systemctl status rc-local.service

相关文章

网友评论

      本文标题:ubuntu18.04设置每次开机自启动后执行临时挂载命令

      本文链接:https://www.haomeiwen.com/subject/evdmedtx.html