原文网址:Ubuntu20.04--开机自动运行脚本(命令)--方法/实例_IT利刃出鞘的博客-CSDN博客 简介 本文介绍ubuntu20.04如何开机自动运行命令。(也适用于Ubuntu22.04版本) 新版本方案(20
原文网址:Ubuntu20.04--开机自动运行脚本(命令)--方法/实例_IT利刃出鞘的博客-CSDN博客
本文介绍ubuntu20.04如何开机自动运行命令。(也适用于Ubuntu22.04版本)
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
创建/etc/rc.local,里边写自己想要运行的命令。例:
#!/bin/shecho "This is test" > /tmp/my.logexit 0
给/etc/rc.local加上可执行权限
sudo chmod +x /etc/rc.local
重启虚拟机
启动后可以发现:/tmp下已经有了my.log文件,里边内容为:"This is test"。
启动服务
sudo systemctl start rc-local.service
查看服务状态
sudo systemctl status rc-local.service
下边的方案在Ubuntu2018及之后的版本无效。
1.创建脚本
新建名为run_all_server.sh,内容如下:
#!/bin/bash# 运行redis服务cd /work/server/redis/nohup redis-server redis.conf > redis.log 2>&1 &
2.添加执行权限
chmod +x run_all_server.sh
3.将脚本放入/etc/init.d路径下
sudo cp run_all_server.sh /etc/init.d/
4.将脚本添加到启动脚本
cd /etc/init.d/update-rc.d run_all_server.sh defaults 90
90的含义:表明优先级,越大表示执行的越晚。
下边的命令会失败:
sudo update-rc.d /etc/init.d/run_all_server.sh defaults 90
错误信息:update-rc.d: error: unable to read /etc/init.d//etc/init.d/run_all_server.sh
5.移除脚本
update-rc.d -f run_all_server.sh remove
修改/etc/r.local
#!/bin/sh -e## 在这里写自己的命令exit 0
一定要将命令添加在exit 0之前。里面可以直接写命令或者执行shell脚本文件sh。
来源地址:https://blog.csdn.net/feiying0canglang/article/details/124695749
--结束END--
本文标题: Ubuntu20.04--开机自动运行脚本(命令)--方法/实例
本文链接: https://www.lsjlt.com/news/392877.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-13
2024-05-13
2024-05-11
2024-05-11
2024-05-10
2024-05-07
2024-04-30
2024-04-30
2024-04-30
2024-04-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0