Using "watch" to run a function repeatedly in Bash?

Allenph

This is my first Bash script. I have a WiFi issue with my Debian machine. I'm not here to ask about the cause, but rather how to put a band-aid on the issue with Bash. My WiFi will drop out at a random time, usually every 12-15 minutes. I use SSH on this server, and do not want to have to run ifdown wlan0 and ifup wlan0 (which reconnects the WiFi) manually from the physical server.

The function of this Bash script is to attempt to connect three times. If it fails three times, it will give up. Otherwise, every three seconds it will check whether or not my server is connected by attempting to ping Google.

#!/bin/bash

ATTEMPTS=1

function test_connection {
  ping -c 1 www.google.com
  local EXIT_CODE=$?
  if [ $EXIT_CODE -eq 0 ]
    then
      return true
    else
      return false
  fi
}
function reset_connection {
  ifdown wlan0
  ifup wlan0
  EXIT_CODE=$((EXIT_CODE+1))
}
function connection_test_loop {
  if [ $ATTEMPTS -ge  3 ]
    then
      echo CONNECTION FAILED TO INITIALIZE ... ATTEMPT $ATTEMPTS FAILED ... EXITING
      exit
  fi
  if ! [ test_connection ]
    then
      echo CONNECTION DROPPED ... ATTEMPTING TO RE-ESTABLISH CONNECTION ... ATTEMPT $ATTEMPTS
      reset_connection
  fi
}

test_connection
if [ $? ]
  then
    echo CONNECTION PRE-ESTABLISHED
    watch -n 3 connection_test_loop
  else
    echo CONNECTION FAILED TO INITIALIZE ... ATTEMPTING TO RESET CONNECTION ... ATTEMPT $ATTEMPTS
    reset_connection
    if [ $? ]
      then
        echo CONNECTION ESTABLISHED
        watch -n 3 connection_test_loop
      else
        echo CONNECTION FAILED TO INITIALIZE ... ATTEMPT $ATTEMPTS FAILED ... EXITING
        exit
    fi
fi

I've isolated the issue I'm having with this script. It lies in calling the connection_test_loop function with watch. I've been unable to find any information as to why this isn't performing as expected and running the function every three seconds.

l'L'l

It's possible that watch is not aware of your connection_test_loop function. You can try adding an export below the test_connection to perhaps solve the issue:

test_connection
export -f connection_test_loop
...

http://linuxcommand.org/lc3_man_pages/exporth.html

When calling watch, you may need this syntax:

watch -x bash -c connection_test_loop

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Run Python script repeatedly from bash

From Dev

How to run a python app repeatedly using Bluemix?

From Dev

How to call the same JavaScript function repeatedly while waiting for the function to run its course before invoking it again, using method chaining?

From Dev

using watch run 2 commands

From Dev

How to repeatedly run bash script every N seconds?

From Dev

How to get watch to run a bash script with quotes

From Dev

How to force watch to run under bash

From Dev

I Want to run the function "getData()" repeatedly inside Service

From Dev

Java - repeatedly run a function in a given number of milliseconds accurately?

From Dev

Create "function" to run bash?

From Dev

Run JAVA program at exact times repeatedly using ScheduledExecutorService

From Dev

On Ubuntu16.04, how to run python script repeatedly - using crontab

From Dev

Actively display elapsed time using watch in bash

From Dev

Bash script using 'watch' fails. Why?

From Dev

R: Defining a function (and/or using apply() or for loop) to perform a set of procedures repeatedly

From Dev

Run custom bash function in Rails

From Dev

How to run bash function in Dockerfile

From Dev

Run custom bash function in Rails

From Dev

Run 1 function of bash script

From Dev

Function using watch on command with command line arguments

From Dev

Repeatedly call a function: Haskell

From Dev

PowerShell Repeatedly calling a function

From Dev

Run a bash script in cygwin using ./

From Dev

Run bash script using name

From Dev

BASH Script Using & in a function

From Dev

BASH Script Using & in a function

From Dev

AngularJS - run task in background repeatedly

From Dev

run runOnUiThread() repeatedly causes exception

From Dev

Run two SKActions repeatedly in sequence?

Related Related

  1. 1

    Run Python script repeatedly from bash

  2. 2

    How to run a python app repeatedly using Bluemix?

  3. 3

    How to call the same JavaScript function repeatedly while waiting for the function to run its course before invoking it again, using method chaining?

  4. 4

    using watch run 2 commands

  5. 5

    How to repeatedly run bash script every N seconds?

  6. 6

    How to get watch to run a bash script with quotes

  7. 7

    How to force watch to run under bash

  8. 8

    I Want to run the function "getData()" repeatedly inside Service

  9. 9

    Java - repeatedly run a function in a given number of milliseconds accurately?

  10. 10

    Create "function" to run bash?

  11. 11

    Run JAVA program at exact times repeatedly using ScheduledExecutorService

  12. 12

    On Ubuntu16.04, how to run python script repeatedly - using crontab

  13. 13

    Actively display elapsed time using watch in bash

  14. 14

    Bash script using 'watch' fails. Why?

  15. 15

    R: Defining a function (and/or using apply() or for loop) to perform a set of procedures repeatedly

  16. 16

    Run custom bash function in Rails

  17. 17

    How to run bash function in Dockerfile

  18. 18

    Run custom bash function in Rails

  19. 19

    Run 1 function of bash script

  20. 20

    Function using watch on command with command line arguments

  21. 21

    Repeatedly call a function: Haskell

  22. 22

    PowerShell Repeatedly calling a function

  23. 23

    Run a bash script in cygwin using ./

  24. 24

    Run bash script using name

  25. 25

    BASH Script Using & in a function

  26. 26

    BASH Script Using & in a function

  27. 27

    AngularJS - run task in background repeatedly

  28. 28

    run runOnUiThread() repeatedly causes exception

  29. 29

    Run two SKActions repeatedly in sequence?

HotTag

Archive