Repeatedly run a shell command until it fails?

空扰寡人 提交于 2019-12-02 13:56:00
nneonneo

while takes a command to execute, so you can use the simpler

while ./runtest; do :; done

This will stop the loop when ./runtest returns a nonzero exit code (which is usually indicative of failure).

To further simplify your current solution though, you should just change your untilfail script to look like this:

#!/bin/bash

while $@; do :; done

And then you can call it with whatever command you're already using:

untilfail ./runTest --and val1,val2 -o option1

If you don't want to wrap a complex pipe line into a shell script or function then this works:

while true; do 
  curl -s "https:..." | grep "HasErrors.:true"
  if [[ "$?" -ne 0 ]]; then 
    break
  fi
  sleep 120
done

The HTTP request in this case always returns 200 but also returns some JSON which has an attribute "HasErrors":true when there is an error.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!