# /etc/init/update-repositories.conf - Update local repos
#
description "Update local repos"
# this will run the script section every time network is up
start on (net-device-up IFACE!=lo)
task
script
svn up && git fetch
# do some other useful stuff
end script
ping 8.8.8.8 -c 1 -i .2 -t 60 > /dev/null 2>&1
ONLINE=$?
if [ ONLINE -eq 0 ]; then
#We're offline
else
#We're online
fi
最近我用过这样的东西:
#!/bin/bash
function check_online
{
netcat -z -w 5 8.8.8.8 53 && echo 1 || echo 0
}
# Initial check to see if we are online
IS_ONLINE=check_online
# How many times we should check if we're online - this prevents infinite looping
MAX_CHECKS=5
# Initial starting value for checks
CHECKS=0
# Loop while we're not online.
while [ $IS_ONLINE -eq 0 ]; do
# We're offline. Sleep for a bit, then check again
sleep 10;
IS_ONLINE=check_online
CHECKS=$[ $CHECKS + 1 ]
if [ $CHECKS -gt $MAX_CHECKS ]; then
break
fi
done
if [ $IS_ONLINE -eq 0 ]; then
# We never were able to get online. Kill script.
exit 1
fi
# Now we enter our normal code here. The above was just for online checking
#! /bin/bash
# This script checks that the interface is up, and that an internet connection is available
# It is based on code from http://askubuntu.com/questions/3299/how-to-run-cron-job-when-network-is-up
#
# Then it sleeps for a random number of seconds between 30 and 600.
# This is based on code from http://tldp.org/LDP/abs/html/randomvar.html
#
# Collated by @JonTheNiceGuy on 2015-10-15
function check_ipaddr
{
# Here we look for an IP(v4|v6) address when doing ip addr
# Note we're filtering out 127.0.0.1 and ::1/128 which are the "localhost" ip addresses
# I'm also removing fe80: which is the "link local" prefix
ip addr | \
grep -v 127.0.0.1 | \
grep -v '::1/128' | \
grep -v 'inet6 fe80:' | \
grep -E "inet [[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+|inet6" | \
wc -l
}
function check_google
{
netcat -z -w 5 8.8.8.8 53 && echo 1 || echo 0
}
until [ `check_ipaddr` -gt 1 ]; do
sleep 2
done
until [ `check_google` -eq 1 ]; do
sleep 2
done
sleep $((RANDOM%570+30))
我认为您可以使用Upstart来帮助您。请注意,我还没有测试过下面的代码是否有效,但应该有一些非常相似的东西。
差不多就是这样。您可能需要添加一些代码来检查它是否不经常运行。您可能还想添加
start update-repositories
到您的 crontab,如果您长时间上网,它将确保您的更新会发生。我做了一个在 DNS 服务器上进行 ping 测试以确保联网的 cron。像这样的东西:
最近我用过这样的东西:
这不是最优雅的 - 我不确定如何通过系统上的简单命令或文件进行检查,但这在需要时对我有用。
您可以与 NetworkManager 交谈以查看您是否已连接:
只是将这里的几个选项包装成一个脚本:
我计划在https://gist.github.com/JonTheNiceGuy/5cf4a23c8f2f755a9ca4维护这个脚本
为了扩展 nixternal,
fping
二进制文件非常适合。你可以用单线把它煮熟,就像如您所见,yoo.mama 不喜欢我,但 Google 喜欢。在 crontab 中,你会做类似的事情
我所做的是创建一个 shell 脚本来满足你的需要,即。检查网络连接,然后启动更新。然后从 cron 调用脚本。
我定义了一个
bash
别名来回答这个问题:您可以使用它,也可以处理
ip link show
自己的输出。要处理“网络关闭/网络出现”,请参阅我在net-o-matic 的
net-o-matic
脚本我发现
ping
解决方案给了我网络错误,这些错误会向我的日志发送垃圾邮件,除非我费力地将错误定向到 /dev/null网络管理器可用于: