【发布时间】:2011-08-09 11:13:28
【问题描述】:
我编写了一个小脚本,它可以 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
真的希望这对某人有所帮助,因为这让我发疯了。感谢所有提供帮助的人。
【问题讨论】:
-
脚本是否在网络启动前运行?
-
afaik rc.local 应该在所有其他初始化脚本之后运行,因此也在网络启动之后运行。尽管如此:您的日志中是否收到 $ADRESS is DEAD 消息?
-
没有。我只收到“从 rc.local 执行网络设备检测脚本”消息。虽然 LIVE/DEAD 消息可以从根终端运行。
-
任何正确的 rc.local 应该是最后运行的脚本列表。