Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have some anacron jobs which run daily. The scripts update local bzr and git repositories. Naturally these scripts need working network connections. I'm on a laptop and often wired and wireless internet do not come up fast enough. This causes my cron job to time out when pulling the repositories =(


How to make sure the internet is up before running specific cron jobs? Or how to fail a job if there is no network, such that it is retried by anacron later again?

share|improve this question
What language? Bash, Python, Perl? –  Marco Ceppi Aug 30 '10 at 1:32
@Marco Ceppi: Any will do. But I don't know Perl =) –  Dima Aug 30 '10 at 22:31

6 Answers 6

up vote 6 down vote accepted

I think you can use Upstart to help you there. Mind you, I haven't tested that code below works but something very similar should.

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


    svn up && git fetch
#   do some other useful stuff
end script

That pretty much it. You might want to add some code to check that it does not run very often. You might also want to add start update-repositories to your crontab, it'll make sure your update will happen if you are on the net constantly for a prolonged period of time.

share|improve this answer
This code will run every time a network interface comes up, including every time your wireless drops and reconnects. If you only want it to run once a day, add logic to place/check a timestamp. –  user535733 Jan 18 '14 at 3:46

I made a cron that did a ping test on a DNS server to ensure networking. Something like this:

ping -c 1 -i .2 -t 60 > /dev/null 2>&1

if [ ONLINE -eq 0 ]; then
    #We're offline
    #We're online

Recently I've used something like this:


function check_online
    netcat -z -w 5 53 && echo 1 || echo 0

# Initial check to see if we're online
# How many times we should check if we're online - prevents infinite looping
# Initial starting value for checks

# Loop while we're not online.
while [ $IS_ONLINE -eq 0 ];do
    # We're offline. Sleep for a bit, then check again

    sleep 10;

    CHECKS=$[ $CHECKS + 1 ]
    if [ $CHECKS -gt $MAX_CHECKS ]; then

if [ $IS_ONLINE -eq 0 ]; then
    # We never were able to get online. Kill script.
    exit 1

# Now we enter our normal code here. The above was just for online checking

This isn't the MOST elegant - I'm not sure how else to check via a simple command or file on the system, but this has worked for me when needed.

share|improve this answer

You can talk to NetworkManager to see whether you are connected or not:

$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!"
    echo "Not connected!"
share|improve this answer
Nice. And ugly :) Seems like a good candidate for implementation in some standard tool for convenient shell access, or via a Python API, so it was more readable and didn't use magic values like "3". –  nealmcb May 18 '11 at 20:10

To expand on nixternal, the fping binary is excellent for that. You can cook it up in one-liners as in

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

As you see, yoo.mama does not like me but Google does. In crontab, you'd do something like

5 5 * * *  root   fping -q google.com && /some/script/I/want --to --run
share|improve this answer
That won't run the command later if the network is down. How could that be accomplished? –  aperson Aug 30 '10 at 5:00

What I do is create a shell script that does what you need, ie. checks for network connection and then fires off the updates. Then call the script from cron.

share|improve this answer
until ifconfig eth | grep -qE 'addr:([[:digit:]]+\.?)+'; do
   sleep 2
share|improve this answer
This answer doesn't quite explain what the user should be doing to resolve their issue. –  TomKat Jan 9 '14 at 9:15

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.