在Bash中使用“watch”重复运行一个函数?

时间:2023-01-18 01:08:50

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.

这是我的第一个Bash脚本。我的Debian机器有WiFi问题。我来这里不是要问原因,而是要问如何用Bash来解决这个问题。我的WiFi会在任意时间退出,通常是每12-15分钟。我在这个服务器上使用SSH,并且不希望运行ifdown wlan0和ifup wlan0(从物理服务器上手动重新连接WiFi)。

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.

这个Bash脚本的功能是尝试连接三次。如果失败三次,它就会放弃。否则,每隔三秒钟,它将通过尝试ping谷歌检查我的服务器是否连接。

#!/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.

我已经将这个脚本的问题隔离开来。它位于使用watch调用connection_test_loop函数。我一直找不到任何信息来解释为什么它不能正常运行,每三秒钟运行一次函数。

1 个解决方案

#1


3  

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

手表可能不知道connection_test_loop函数。您可以尝试在test_connection行下面添加一个导出,以解决以下问题:

test_connection
export -f connection_test_loop
...

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

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

#1


3  

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

手表可能不知道connection_test_loop函数。您可以尝试在test_connection行下面添加一个导出,以解决以下问题:

test_connection
export -f connection_test_loop
...

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

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