Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm writing a shell script that brings up an Ethernet interface (configured for DHCP) to determine whether it's connected to a network. If it isn't, I'd like to be able to report in a timely manner (within a few seconds) that there's no connection. Unfortunately, in the absence of a connection, ifup seems to wait several minutes before timing out, and the man page doesn't mention a timeout parameter. Is there any way to stop it after a few seconds?

This is on Ubuntu Server 11.10; NetworkManager is not in use.

share|improve this question

You did not say what you are using for a notification.

[[ -n "$(ifconfig | grep '192.*' )" ]] || echo "down"

Change "192" to the prefix of your network (192.168.x.y is most common).

Change "echo "down"" to your notification.

Run that command from cron with some reasonable interval.

Perhaps something along the lines of

[[ -n "$(ifconfig | grep '192.*' )" ]] || dhclient eth0

run once a minute or so ?

share|improve this answer
This isn't a periodic notification thing. It's part of a network setup process, and I'm just trying to figure out "is eth0 physically connected to a network or not?" – Taymon Jan 6 '12 at 6:38
up vote 0 down vote accepted

I finally found a way.

As it turns out, since what I really want to do is check for link, I don't need ifup at all. Instead, I can use ethtool (though it has to be installed first), like so:

connected=$(ethtool eth0 | awk '/Link detected:/ { print $3 }')

If, however, I did need to check ifup, I could use a background process and the $! shell variable. If I just wanted to check whether it was still running after the timeout, I could do this:

ifup eth0 &
sleep 5
if ps -p $! | grep -q "$!"; then
    kill $!

If I wanted to capture the output from ifup, my best bet would probably be to redirect it to a temporary file. (I looked into named pipes, but I don't think they'd work for this purpose; ifup would block until another process was reading its output.)

share|improve this answer

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.