linux rc.local 权限,Linux rc.local 命令不执行-Fun言

米扑科技,这两天需要在Ubuntu中配置开机启动的命令,有很多方式

在rc.local中配置是比较简单方便的一种,所以打算使用rc.local的方式进行配置。

简介

/etc/rc.local 实际上是一个文件软链接,指向真实的文件是 /etc/rc.d/rc.local

# ll /etc/rc.local

lrwxrwxrwx. 1 root root 13 8月 14 00:20 /etc/rc.local -> rc.d/rc.local

# ll /etc/rc.d/rc.local

-rw-r--r--. 1 root root 615 11月 21 10:18 /etc/rc.d/rc.local

开机脚本不执行(一)

有时,在 /etc/rc.local 文件里设置脚本后,发现不执行脚本,原因是没有给 /etc/rc.d/rc.local 授权为可执行

如上,发现 /etc/rc.local(软链文件)是可执行状态,但是 /etc/rc.d/rc.local(真实文件)不是可执行状态

因此,授权 /etc/rc.d/rc.local为可执行状态:

chmod +x /etc/rc.d/rc.local

以后设置脚本也直接放到 /etc/rc.d/rc.local里,且脚本也设置为可执行状态:

chmod +x /root/script/xxxx.sh

开机脚本不执行(二)

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(如下)

$ ls -al /bin/sh

lrwxrwxrwx 1 root root 4  2月 19  2014 /bin/sh -> dash

$ls -al /bin/dash

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

$ echo $SHELL

/bin/bash

而/etc/rc.local脚本中用的正是/bin/sh,导致命令无法运行。

$ head /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.

解决方案:

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

sudo reboot 系统后,命令正常执行

最后注意,如果你在rc.local 中执行脚本的话,

必须使用sudo chmod +x 给脚本增加执行权限。

sudo chmod +x /home/homer/script/mimvp_proxy.sh

有很多人可能会忽略这个问题。

总结

以上方法,在我的Ubuntu 14.04系统反复配置多次,仍然不行,开机无法执行

1. 把 /etc/rc.local 改名自定义的 /etc/rc_mimvp.sh

sudo cp /etc/rc.local /etc/init.d/rc_mimvp.sh

sudo chmod +x /etc/init.d/rc_mimvp.sh

2. 将脚本添加到启动脚本

cd /etc/init.d/

sudo update-rc.drc_mimvp.shdefaults 90

3. 如果要移除启动脚本

sudo update-rc.d -frc_mimvp.shremove

参考推荐:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值