网络启动时如何运行cron作业?

我有一些日常工作的anacron工作。 脚本更新本地bzr和git存储库。 当然,这些脚本需要有效的网络连接。 我在笔记本电脑上,经常有线和无线上网速度不够快。 这会导致我的cron作业在拉出存储库时超时=(

所以:

如何在运行特定的cron作业之前确保互联网已启动? 或者,如果没有网络,如何失败,以便以后再由anacron重试?

我想你可以使用Upstart来帮助你。 请注意,我没有测试下面的代码,但是非常相似。

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

那就是它。 您可能希望添加一些代码以检查它是否经常运行。 您可能还想将start update-repositories添加到您的crontab中,如果您长时间在网络上,它将确保您的更新将会发生。

我做了一个在DNS服务器上进行ping测试的cron,以确保网络连接。 像这样的东西:

 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 

这不是最优雅的 – 我不确定如何通过系统上的简单命令或文件进行检查,但这在需要时对我有用。

您可以与NetworkManager通信以查看您是否已连接:

 $state = $(dbus-send --system --print-reply \ --dest=org.freedesktop.NetworkManager \ /org/freedesktop/NetworkManager \ org.freedesktop.NetworkManager.state 2>/dev/null \ | awk '/uint32/{print $2}') if [ $state = 3 ]; then echo "Connected!" else echo "Not connected!" fi 

只是将这里的几个选项包装成一个脚本:

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

我打算在https://gist.github.com/JonTheNiceGuy/5cf4a23c8f2f755a9ca4维护这个脚本

为了扩展nixternal, fping二进制文件非常适合。 你可以用单行烹饪方式烹饪

 $ fping -q yoo.mama && echo yes $ fping -q www.google.com && echo yes yes $ 

如你所见,yoo.mama不喜欢我,但Google确实如此。 在crontab中,你会做类似的事情

 5 5 * * * root fping -q google.com && /some/script/I/want --to --run 

我所做的是创建一个shell脚本来完成你需要的东西,即。 检查网络连接,然后触发更新。 然后从cron调用脚本。

 until ifconfig eth | grep -qE 'addr:([[:digit:]]+\.?)+'; do sleep 2 done 

我已经定义了一个bash别名来回答这个问题:

 alias netstate='ip link show | egrep -q '\''UP,LOWER_UP.* state UP'\'' && echo UP || echo DOWN' 

您可以使用它,或者自己处理ip link show的输出。

要处理“net down down / net come up”,请参阅net-o-matic脚本

我发现ping解决方案给了我网络错误,这些错误会使我的日志发送垃圾邮件,除非我费力地将错误导向/ dev / null

可以使用网络管理器:

 if nmcli general | grep "^connected" >/dev/null then echo "nmcli connected" fi