在Internet关闭时运行进程并暂停它,在启动时继续循环

时间:2018-01-10 10:01:29

标签: bash shell timeout connection-timeout

我有一个对超时过于敏感的命令,以及一个糟糕的Internet连接,它给了我多秒的无连接时间,这种连接只会频繁导致该命令失败。

我想要一个表单的Bash脚本:

  • tubeify -t 100ms -- /usr/bin/somecommand with its arguments(用于运行新流程)或
  • tubeify -t 1s $PID(已启动的流程)

...这样somecommand将自动STOP,而'net is down(即ping 8.8.8.8 [vel sim]超过-t值,并且CONT当它重新启动时。{/ p>

或者,或者像现在已经不存在的airhook程序一样,它会强制连接保持打开状态,而不会以某种方式超时使用服务器端的配套应用程序,该应用程序在本地客户端关闭时中继丢失的数据包。

Bash loop ping successful提供了一个组件,用于检测互联网状态。

我认为这样的程序已经存在,但是无法在googs上找到它,所以也许那些比我更好的人可以通过一个简单的链接回答已经做过的事情。

1 个答案:

答案 0 :(得分:0)

这是在作业1上运行的半版本(即运行somecommand,然后使用ctl-z进行背景,假设它是唯一的一个):

next=0;
while true;
do
  kill -SIGINFO %1;
  alive=$?;
  if [[ $alive -ne 0 ]];
  then
    say 'job not found';
    break;
  fi;
  ping -noc 1 -t 1 8.8.8.8 > /dev/null;
  rc=$?;
  last=$next;
  next=$rc;
  if [[ $rc -eq 0 ]];
  then
    if [[ $last -ne 0 ]];
    then
      kill -CONT %1;
      say up;
    fi;
  else
    if [[ $last -eq 0 ]];
    then
      kill -STOP %1;
      say down;
    fi;
  fi;
  sleep 0.1;
done

1行版本,方便copypasta:

next=0; while true; do kill -SIGINFO %1; alive=$?; if [[ $alive -ne 0 ]]; then say 'job not found'; break; fi; ping -noc 1 -t 1 8.8.8.8 > /dev/null; rc=$?; last=$next; next=$rc; if [[ $rc -eq 0 ]]; then if [[ $last -ne 0 ]]; then kill -CONT %1; say up; fi; else if [[ $last -eq 0 ]]; then kill -STOP %1; say down; fi; fi; sleep 0.1; done

它有效,但有点像kludge。不确定它是否也可以作为.sh