BASH kill wget if no response
I have this code
...
SERVERCONNECTION=$(wget --quiet -O - http://xx:yy@127.0.0.1:10001/server | grep connections | awk '{print $36}')
Sometimes the url get inresponsive, then I want to kill wget process and set SERV开发者_如何学GoERCONNECTIION variable to 0.
Set a timeout for the wget process with --timeout=seconds
, i.e.
SERVERCONNECTION=$(wget --timeout=5 --quiet -O - http://xx:yy@127.0.0.1:10001/server | grep connections | awk '{print $36}')
Another useless use of grep.
Use awk '/connections/ {print $36}'
instead, so that the whole line reads
wget --timeout=5 --quiet -O - http://xx:yy@127.0.0.1:10001/server | awk '/connections/ {print $36}'
精彩评论