Ubuntu中rc.local 中命令不执行

这两天需要在Ubuntu中配置开机启动的命令,有很多方式,在rc.local中配置是比较简单方便的一种,所以打算使用rc.local的方式进行配置。

我配置完后,却发现配置的sh脚本始终不执行,开始怀疑是rc.local没执行,用echo打印了些信息,发现都执行了。

这就造成了我的困惑,网上查了好多解决方案,试了好多种方法,最终发现是有shell的问题造成的。有些命令需要在bash shell中运行,而不能在dash中运行。

从Ubuntu 6.10开始,默认使用dash(theDebian Almquist Shell)而不是bash(the GNUBourne-Again Shell). 但Login Shell还是bash。

使用命令查了下,看到/bin/sh链接到了/bin/dash(如下),而/etc/rc.local脚本中用的正是/bin/sh,导致命令无法运行。


robotech@robotech-ros:~$ echo $SHELL

/bin/dash

robotech@robotech-ros:~$ ls -al /bin/sh

lrwxrwxrwx 1 root root 4 10月 10 10:51 /bin/sh ->dash

 

robotech@robotech-ros:~$ ls -al /bin/dash

-rwxr-xr-x 1 root root 121272  2月 19  2014 /bin/dash


解决方案:

1.将/etc/rc.local的命令改成更加兼容的模式,将"#!/bin/sh"改为"#!/bin/bash" 

2.将/bin/sh重新链接到/bin/bash,方法如下:

方法一:终端执行 sudo dpkg-reconfigure dash,然后选择 no.

方法二:重新进行软链接,执行以下命令:

sudo rm /bin/sh
sudo ln -s /bin/bash /bin/sh

这样就将/bin/sh链接到了/bin/bash。reboot系统后,命令正常执行。


非常感谢这个文档给我解决了问题,网址:http://www.cnblogs.com/liulanghe/p/3376380.html


最后需要注意一点,如果你在rc.local 中执行脚本的话,别忘了使用sudo chmod +x 给脚本增加执行权限。有很多人可能会忽略这个问题。

  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Ubuntu 22.04,如果你想要将一个程序添加到开机启动,你可以通过修改rc.local文件来实现。在Ubuntu,有两个rc.local文件,分别是/etc/rc.local和/etc/init.d/rc.local。这两个文件有一定的关联关系。 如果你在系统找不到这两个文件,你可以自己创建一个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 然后,你可以启动rc-local.service服务并将其设置为开机自启: sudo systemctl start rc-local.service sudo systemctl enable rc-local.service 最后,你可以创建/etc/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. exit 0 记得给予脚本执行权限。通过以上步骤,你可以将程序添加到Ubuntu 22.04的rc.local,并实现开机启动。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [ubuntu系统/etc/rc.local和/etc/init.d/rc.local的区别详解](https://download.csdn.net/download/weixin_38564826/14890316)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [ubuntu没有rc.local文件](https://blog.csdn.net/qq_35002542/article/details/127999561)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值