Linux 如何知道连接被拒绝了

Linux 如何知道连接被拒绝了,linux,bash,curl,Linux,Bash,Curl,在shell脚本中,我必须通过shell脚本中的curl命令调用一些服务作为url。在这种情况下,如果服务未运行,则curl表示连接被拒绝,如何获取此异常并调用另一台计算机上运行的服务。您可以检查curl的退出状态。例如: curl -I -A "Chrome" -L "http://localhost:8080" curl: (7) Failed connect to localhost:8080; Connection refused [[ $? -eq 7 ]] && cu

在shell脚本中,我必须通过shell脚本中的curl命令调用一些服务作为url。在这种情况下,如果服务未运行,则curl表示连接被拒绝,如何获取此异常并调用另一台计算机上运行的服务。

您可以检查
curl
的退出状态。例如:

curl -I -A "Chrome" -L "http://localhost:8080"
curl: (7) Failed connect to localhost:8080; Connection refused
[[ $? -eq 7 ]] && curl -I -A "Chrome" -L "http://localhost:9090"

您可以通过curl检查进程退出代码。供参考

curl  "http://www.my.damn.unreachable.server.com:9090"
if [ "$?" = "7" ]; then 
    echo 'connection refused or cant connect to server/proxy';
fi