I just need to run a shell script when an internet connection is available. Is it possible to watch the internet connection availability through shell scripting?
ping google.com
is the easy command to get it working. But how can I watch it?
I just need to run a shell script when an internet connection is available. Is it possible to watch the internet connection availability through shell scripting?
ping google.com
is the easy command to get it working. But how can I watch it?
I think you might just be looking for
ping -c1 -W1 google.com >/dev/null && command
That will execute command
only if the ping was successful.
If watching means looking at transfer speeds, I have two solutions.
ifstat -S
sudo iftop
You could do something like this
#!/bin/bash
while [ 1 ]; do
TEST=$(nmcli -f STATE dev status | awk '/connected/ {print 1}')
if [ $TEST ]; then
# place whatever you wanna do here if connection is available
fi
sleep 0.25
done
nmcli
has command dev status
that shows the status of network interfaces. So, we check only the status column of the output with the -f STATUS
format flag. If we have connection, regardless if that's ethernet or wifi, we set TEST variable to 1 , meaning true. Next is if . . then . . .fi
statement, which actually does the job.