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?


3 Answers 3


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.

Speed of interfaces(wifi, ethernet)

ifstat -S

Speed of individual transfers/connections

sudo iftop


You could do something like this


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
sleep 0.25

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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