BASH SCRIPT :代码可以从终端运行,但不能从 rc.local 运行

发布于 2024-11-28 15:35:47 字数 1494 浏览 1 评论 0原文

我编写了一个小脚本,它 ping 是一个地址,如果 ping 返回成功,则将设备安装到该地址。该文件位于 Ubuntu Linux 系统上的 rc.local 中。

如果从终端(以 root 身份)运行,它会很好用,但不会在启动时从 rc.local 运行。我知道它正在执行,因为 /tmp/buffalo_mount.log 包含“从 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.

ADDRESS=192.168.1.101
DATETIME="$(date)"
LOGFILE="/tmp/buffalo_mount.log"

sleep 30s

echo "Executing Network Device Detect Script From rc.local" >> $LOGFILE
    if /bin/ping -c 1 -t 1 $ADDRESS > /tmp/ping 2>&1 ;then  # check the exit code
        echo "$ADDRESS is LIVE  "+$DATETIME >> $LOGFILE # display the output
    # Ping reply was good, so run the mount command.
    echo "Slept, now mounting device" >> $LOGFILE
    /bin/mount /media/Buffalo/Acer-laptop-back_in_time
    else
        echo "$ADDRESS is DEAD  "+$DATETIME >> $LOGFILE
fi

然后我必须编辑“/etc/fstab”文件,以便 fstab 知道安装情况,但在我的通知之前不会安装上面的脚本,使用“noauto”参数。我在 fstab 中的例子是:-

//192.168.1.101/back-in-time/ /media/Buffalo/Acer-laptop-back_in_time cifs **noauto**,guest,uid=1000,iocharset=utf8,codepage=unicode,unicode,_netdev  0  0

真的希望这能帮助别人,因为它让我发疯。感谢所有提供帮助的人。

I've written a small script that ping's an address and then mounts the device at that address if the ping returned successful. The file is located in rc.local on a Ubuntu Linux System.

It works great if run from a terminal (as root), but won't run from rc.local at boot. I know it's being executed because /tmp/buffalo_mount.log contains "Executing Network Device Detect Script From rc.local". Anyone got any ideas?

NOTE: Now working! Please read notes below :-)

#!/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.

ADDRESS=192.168.1.101
DATETIME="$(date)"
LOGFILE="/tmp/buffalo_mount.log"

sleep 30s

echo "Executing Network Device Detect Script From rc.local" >> $LOGFILE
    if /bin/ping -c 1 -t 1 $ADDRESS > /tmp/ping 2>&1 ;then  # check the exit code
        echo "$ADDRESS is LIVE  "+$DATETIME >> $LOGFILE # display the output
    # Ping reply was good, so run the mount command.
    echo "Slept, now mounting device" >> $LOGFILE
    /bin/mount /media/Buffalo/Acer-laptop-back_in_time
    else
        echo "$ADDRESS is DEAD  "+$DATETIME >> $LOGFILE
fi

Then I had to edit the '/etc/fstab' file so the fstab knows about the mount, but doesn't mount until told to by my script above, using the 'noauto' parameter. My example in fstab is:-

//192.168.1.101/back-in-time/ /media/Buffalo/Acer-laptop-back_in_time cifs **noauto**,guest,uid=1000,iocharset=utf8,codepage=unicode,unicode,_netdev  0  0

Really hope this help someone because it was driving me nuts. Thanks to all those that helped.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒废 2024-12-05 15:35:47

如果命令的存在状态非 0,则 -e 参数要求 sh 退出,因此脚本将停止而不是执行 if 的 else 分支。你应该替换

/bin/ping -c 1 -t 1 $ADDRESS > /dev/null 2> /dev/null  # ping and discard output
if [ $? -eq 0 ] ; then

if /bin/ping -c 1 -t 1 $ADDRESS > /dev/null 2>&1 ; then

The -e argument asks sh to exit if a command have a non 0 exist status, thus the script will stop instead of executing the else branch of your if. You should replace

/bin/ping -c 1 -t 1 $ADDRESS > /dev/null 2> /dev/null  # ping and discard output
if [ $? -eq 0 ] ; then

by

if /bin/ping -c 1 -t 1 $ADDRESS > /dev/null 2>&1 ; then
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文