Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

E.g. system: Ubuntu/Debian.

As many of you do this probably via ping and a terminal, I always forget this terminal when switching to other task. So a notification pop-up would be useful. So can I do better as this?

while; do
  if ping -c 1 your.host.com; expr $? = 0; then
     notify-send "your.host.com back online"; sleep 30s;
  else
     sleep 30s;
  fi;
done

You will need zsh and libnotify to let the snippet work. As script:

#!/usr/bin/env zsh
while; do if ping -c 1 $1; expr $? = 0; then notify-send "$1 back online"; sleep 30s; else sleep 30s; fi; done
share|improve this question
    
Quick refactoring suggestion: make it a function which accepts the hostname/IP address as argument. –  vtest Oct 15 '10 at 8:56
add comment

1 Answer

up vote 2 down vote accepted

The idea looks right to me. By using while :; do ... you can make it portable to normal Bourne shells. The expr calls seems unnecessary. Also, you probably want to break out of the loop when the host is found.

while :; do
    if ping -c 1 $1; then
        notify-send "$1 back online"
        break
    fi
    sleep 30s
done
share|improve this answer
add comment

Your Answer

 
discard

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.