我需要等待特定条件满足后网络接口才被视为“启动”。Upstartnet-device-up
在满足此条件之前发送信号,这会导致依赖此条件的脚本失败。如何延迟 Upstart 的 net-device-up 信号直到条件满足?
答案1
一种解决方案是放入一个脚本,/etc/network/if-up.d/
该脚本在任何其他 if-up.d 脚本之前执行,并等待特定条件满足或发生超时。例如,这是一个脚本,用于等待使用“自动”方法配置的 IPv6 接口上的非链路本地 IPv6 地址:
#!/bin/sh
LOG_FILE="/tmp/autoipv6.log"
if [ "$ADDRFAM" = "inet6" -a "$METHOD" = "auto" ]; then
echo "Auto method detected, waiting for IP address" >> $LOG_FILE
#wait until we have an IP address, or a timeout occurs
TIMEOUT=50 #5 second timeout--each iteration sleeps for 1/10th of a second
#get the number of addresses, excluding link-local addresses
ADDRESS_COUNT=`ip addr show dev eth0 | grep inet6 | grep -v fe80 | wc -l`
until [ $TIMEOUT -eq 0 ]; do
NEW_ADDRESS_COUNT=`ip addr show dev eth0 | grep inet6 | grep -v fe80 | wc -l`
# break out of the loop if more IPv6 addresses have been assigned
# this is implementation is naive: no guarantee is given that the new addresses
# are the result of the autoconfiguration request
if [ "$NEW_ADDRESS_COUNT" -gt "$ADDRESS_COUNT" ]; then
echo "Detected new IP address, exiting" >> $LOG_FILE
ifconfig >> $LOG_FILE
exit 0;
fi
TIMEOUT=$((TIMEOUT-1))
sleep .1
done
echo "Timeout waiting for IP address" >> $LOG_FILE
exit 1;
fi
这个“等待条件”脚本的运行很重要前if-up.d Upstart 脚本运行(Upstart if-up.d 脚本是net-device-up
生成信号的地方)。 if-up.d 脚本由以下脚本执行:运行部件按词汇排序顺序(与 ls 命令的默认输出顺序相同),因此保证顺序的一个简单技巧是将脚本命名为“000wait-for-condition”。