1

I am having the following script that monitors when the connection appears => open chrome with a specific url:

#!/bin/sh
function online {
  wget -q -O /dev/null --timeout=5 http://URL/
  return $?
}

until online
do
  sleep 5
done


google-chrome --start-fullscreen --incognito "http://URL" &

Now I would like to monitor if connection is lost => kill chrome. What sould be the script for that?

Tried the below, but it's not the right syntax

#!/bin/sh
function offline {
  wget -q -O /dev/null --timeout=5 http://URL/
  return !$?
}

while offline
do
  pkill chrome
  sleep 5
done
  • where is it better o keep it? – ihtus Nov 21 '17 at 20:56
  • 1
    @ihtus, this question is OK here, but it's rude to post the same question on multiple sites. – glenn jackman Nov 21 '17 at 21:02
  • 1
    ok, deleted there, kept here – ihtus Nov 21 '17 at 21:05
  • 1
    As glenn demonstrated (but did not explain) in his answer, you don’t need to say return $? at the end of a function — the default return value from a function is the exit status of the last command executed.  (And, if you’re going to reference $?, you should put it into quotes — "$?".) – G-Man Says 'Reinstate Monica' Nov 21 '17 at 21:43
4

I'd extend your "launch" script:

#!/bin/sh
url="http://URL/"

online() {
  wget -q -O /dev/null --timeout=5 "$url"
}

# infinite loop
while :; do

    # launch chrome when we go online
    until online; do sleep 5; done
    google-chrome --start-fullscreen --incognito "$url" &

    # kill chrome when we go offline
    while online; do sleep 5; done
    pkill chrome

done
  • This is untested. – glenn jackman Nov 21 '17 at 21:17
  • 2
    Looks good. Since the shebang is /bin/sh, though, one might replace function online { with online() { in order to be POSIX compatible. – John1024 Nov 21 '17 at 21:23
-1
I have included both the conditions in script

wget - -spider “http:url”
If [[ $? == 0 ]]
Then
google-chrome --start-fullscreen --incognito "http://URL" &

Else
Ps -eaf | grep -i chrome | awk ‘{print “kill -9” “ “ $2}’ | sh

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.