BASH SCRIPT:代码从终端工作,但不从rc.local工作

and*_*poo 1 linux bash ubuntu ping

我写了一个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
Run Code Online (Sandbox Code Playgroud)

然后我必须编辑'/ etc/fstab'文件,以便fstab知道mount,但是在我上面的脚本告诉我使用' 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
Run Code Online (Sandbox Code Playgroud)

真的希望这有助于某人,因为它让我疯了.感谢所有帮助过的人.

APr*_*mer 6

如果命令具有非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
Run Code Online (Sandbox Code Playgroud)

通过

if /bin/ping -c 1 -t 1 $ADDRESS > /dev/null 2>&1 ; then
Run Code Online (Sandbox Code Playgroud)