Ubuntu: wait for network link up and execute a bash command
The event listener I suggested:
inotifywait -e modify /sys/class/net/eth0/carrier; echo 'Change detected'
When you plug or unplug network cable, it will trigger echo 'Change detected', of course it could trigger just about anything.
And this will run as one off, but I take you know how to make a daemon out of it, if not it will be a good exercise to learn :)
Waiting for network link to be up before continuing in bash
You can check if the network interfaces' names appear after running ifconfig -s
.
Something like:
if [ $( ifconfig -s | grep eth0 ) ]; then echo "eth0 is up!"
Check this link for more details.
To make this test ongoing, you can do something like what @jm666 said:
while ! ping -c 1 -W 1 1.2.3.4; do
echo "Waiting for 1.2.3.4 - network interface might be down..."
sleep 1
done
Wait for Network Interface Before Executing Command
This command should wait until it can contact google or it has tried 50 times:
for i in {1..50}; do ping -c1 www.google.com &> /dev/null && break; done
The for i in {1..50}
loops 50 times or until a break
is executed. The ping -c1 www.google.com
sends 1 ping packet to google, and &> /dev/null
redirects all the output to null, so nothing is outputed. && break
executes break only if the previous command finished successfully, so the loop will end when ping
is successful.
Linux: execute a command when network connection is restored
You can start a script after the linux box gets connected using up
(requires ifplugd
to be installed )
#/etc/network/interfaces
auto eth0
iface eth0 inet dhcp
up /etc/network/yourscript.sh
However, there keep in mind that if you disconnect the cable (and plug in in after a while), the script also starts even though the router might not have been restarted.
--edit--
alternatively , place your script in
/etc/network/if-up.d/
(make sure it is executable and restart networking after changes.)
Bash script to bring up and down an interface on loop
try this one
if ping succeeds then bring $iface
up
if ping fails then bring $iface
down
#!/bin/bash
timeout=3 # delay between checks
iface="eth0" # which interface to bring up/down
pingip='8.8.8.8' # what to ping
isdown=-1 # indicate whether the interface is up(0) or down(1)
# start in unknown state
while true; do
if ping -q -c 2 "$pingip"; then # if ping is succeeds bring iface up
if [ "$isdown" -ne 0 ]; then # if not already up
ifconfig "$iface" up && isdown=0
printf ":: iface brought up: %s\n" "$iface"
fi
elif [ "$isdown" -ne 1 ]; then # if ping failed, bring iface down, if not already down
ifconfig "$iface" down && isdown=1
printf ":: iface brought down: %s\n" "$iface"
fi
sleep "$timeout"
done
Hourly network device restart using Bash in Ubuntu
If you want to write a shell script to this that is up to you, but I am a big fan of reusing something that already exists.
Use cron to run your script every hour. Set it up for the root account and have it mess with the interface.
How to create a loop in bash that is waiting for a webserver to respond?
Combining the question with chepner's answer, this worked for me:
until $(curl --output /dev/null --silent --head --fail http://myhost:myport); do
printf '.'
sleep 5
done
How to wait for an open port with netcat?
You can't set netcat to wait until some port is open, so you have to add part for waiting before next check is made. Try this:
#!/bin/bash
echo "Waiting jenkins to launch on 8080..."
while ! nc -z localhost 8080; do
sleep 0.1 # wait for 1/10 of the second before check again
done
echo "Jenkins launched"
How to wait in bash for several subprocesses to finish, and return exit code !=0 when any subprocess ends with code !=0?
wait
also (optionally) takes the PID
of the process to wait for, and with $!
you get the PID
of the last command launched in the background.
Modify the loop to store the PID
of each spawned sub-process into an array, and then loop again waiting on each PID
.
# run processes and store pids in array
for i in $n_procs; do
./procs[${i}] &
pids[${i}]=$!
done
# wait for all pids
for pid in ${pids[*]}; do
wait $pid
done
Related Topics
Xargs Sh -C Skipping the First Argument
How to Store Linux Command Output into a Variable in Puppet
Termios Vmin Vtime and Blocking/Non-Blocking Read Operations
Remove All Text from Last Dot in Bash
What Are Some Conditions That May Cause Fork() or System() Calls to Fail on Linux
Getting Meteor 0.9.2 Build to Work Osx -> Linux
What's the Max File Mapping Size in 64Bits MAChine
Write to a File After Piping Output from Tail -F Through to Grep
Switching Users Using Winscp Between Different Accounts
How to Grep While Avoiding 'Too Many Arguments'
Suppress or Prevent Duplicate Inotifywait Events
How to Disassemble a System Call
Bash: How to Pass in Arguments to an Alias: Cannot Use a Function - Syntax of Bash Conditionals