0

I'm working on a bash script, opening a yad window and keeping it opened until internet connection is present and then closing it.

It looks as follow:

#!/bin/bash

yad_wind1_id=""
yad_wind1_id_txt_str=""
NET_STATE=0

function test_net_connection()
{
    (ping -c 1 www.google.com &>/dev/null)

    XCODE=$?

    if [ $XCODE == 0 ]; then
        NET_STATE=1
    else
        NET_STATE=0
    fi
}


function kill_yad_proc()
{

     kill_proc=$(ps aux | grep "$1" | grep -v "grep $1" | \
     awk '{print $2}' | sed 's:^:kill :')

     eval $kill_proc

}

function yad_proc_id_string()
{

        yad_win_id=`echo $(date +%F-%H-%M-%6N) | \
        sed 's/-/_/g' | sed 's/^/text=/'`

        yad_win_id_txt_str="--""$yad_win_id"

}

yad_proc_id_string

# ...and here starts my problem...

while [[ $NET_STATE == 0 ]]; do

    test_net_connection

    if [[ $XCODE == 0 ]]; then
            NET_STATE=1
            kill_yad_proc "$yad_win_id"
            break
            exit
    else
            NET_STATE=0
            continue
    fi

done | `yad --fixed --skip-taskbar --undecorated \
    "$yad_win_id_txt_str" \
    --text="Waiting for internet connection..." \
    --button='Quit:bash -c "kill -USR1 $YAD_PID"' \
    --image=disconnected.png`

In this version script works just partly: when internet is off, yad window appears and disappears when internet is on (ping returns 0). Unfortunately, when I press [Quit] button or [escape] (however I'd like to keep this option), it doesn't work as I'd like it to.

What should I add/change? Where the mistake is done?

ludvick
  • 11
  • 2
  • Maybe I missed where in your code there is some wait occurring, but I think your main loop continuously tests the connexion. If it is the case, you may want to introduce a bit of wait (e.g. `sleep 10` or whatever interval you prefer) to avoid excessive resource usage. – Fred Jan 28 '17 at 01:02
  • Could you describe what happens when it doesn't work as you would like it to? – Fred Jan 28 '17 at 01:05
  • At the end of your script, there is a pipe to what seems to be a `yad` command in backticks, denoting a command substitution. Is this the command you are using to launch your window upon starting the script, and which you want to close when the Internet connection returns? – Fred Jan 28 '17 at 01:09
  • Thanks Fred for your comments and suggestions. – ludvick Jan 28 '17 at 01:33
  • Yes, yad launches for me a window and I want to close it. Script works partly, it means when internet is off, window appears. Then, after connecting network, window closes (functions do that). Unfortunately, when I press [Quit] button or [escape] window closes, but script doesn't end (I have to press [ctrl]+[c] on keyboard to break it). – ludvick Jan 28 '17 at 01:41
  • Sorry, I am unable to help you with how your terminal window interacts with your script. – Fred Jan 28 '17 at 01:45
  • Fred you're great! I added 'sleep 1' to the script and everything works as it should! Thanks a lot! – ludvick Jan 28 '17 at 01:45
  • Happy this helped. – Fred Jan 28 '17 at 01:46
  • @ludvick I noticed you don't sign on very often but the next time you do would you please write an answer to this question? It will help others with the similar issues. The comments state the solution but others may not read that far. Thank you :) – WinEunuuchs2Unix Apr 03 '17 at 01:45

1 Answers1

0

"when I press [Quit] button or [escape] (however I'd like to keep this option)" I guess yad is outside loop and this is your problem.

TIP. - I do not see sense to check the connection with ping. But maybe you need and maybe this is best way. Best should be find file inside system for internet conection status and use inotifywait however inotify sometimes is also buggy so you should be carefully. - You can add own icon tray with yad, but you need create two functions with yad with other icons and title. When inetrnet status will change, you can kill first yad window and open next window. This can be in "while" loop when status internet is tested.

guest
  • 1