转载:Ubuntu 开机自动运行脚本(适用于Ubuntu20.04版本及之后)

发布时间 2023-09-14 21:19:33作者: 江浸月

Ubuntu 开机自动运行脚本(适用于Ubuntu20.04版本及之后)

原文网址:Ubuntu20.04--开机自动运行脚本(命令)--方法/实例_IT利刃出鞘的博客-CSDN博客

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