在shell脚本中,我必须通过shell脚本中的curl命令将一些服务作为url调用。在这种情况下,如果服务没有运行,那么curl表示连接被拒绝,我该如何获取此异常并调用在另一台机器上运行的服务。
答案 0 :(得分:2)
您可以检查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"
答案 1 :(得分:2)
您可以通过curl检查进程退出代码。仅供参考http://curl.haxx.se/libcurl/c/libcurl-errors.html
curl "http://www.my.damn.unreachable.server.com:9090"
if [ "$?" = "7" ]; then
echo 'connection refused or cant connect to server/proxy';
fi