美文网首页
Ubuntu20.04-开机自动运行脚本(命令)--方法/实例

Ubuntu20.04-开机自动运行脚本(命令)--方法/实例

作者: 404d67ac8c12 | 来源:发表于2022-12-07 11:31 被阅读0次

本文介绍Ubuntu20.04如何开机自动运行命令。

1.创建rc-local.service文件

sudo cp /lib/systemd/system/rc-local.service /etc/systemd/system
然后修改/etc/systemd/system/rc-local.service,在文件最下方添加如下两行:

[Install]

[WantedBy=multi-user.target]

Alias=rc-local.service

2.创建rc.local文件

创建/etc/rc.local,里边写自己想要运行的命令。例:

#!/bin/sh
echo "This is test" > /tmp/my.log
exit 0

/etc/rc.local加上可执行权限

sudo chmod +x /etc/rc.local

3.测试

重启虚拟机
启动后可以发现:/tmp下已经有了my.log文件,里边内容为:"This is test"。

systemctl命令启动服务
sudo systemctl start rc-local.service

查看服务状态
sudo systemctl status rc-local.service

相关文章

网友评论

      本文标题:Ubuntu20.04-开机自动运行脚本(命令)--方法/实例

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