【问题标题】:Exiting shell script if script throws error如果脚本抛出错误,则退出 shell 脚本
【发布时间】:2019-03-13 14:25:15
【问题描述】:

我有一个 curl 命令启动或停止进程。当我运行它以启动/停止服务时,脚本按预期工作,但是当进程已经启动并且我尝试启动它时它会抛出异常再次。如何优雅地退出?

脚本:

x=$1
action=`echo "$x" | tr '[:upper:]' '[:lower:]'`
y=$2
#echo "ID passed is $y"

echo $action"ing the process..."

curl -s -S -k -u user:pass -X PUT https://URL/$action || exit 1

如果进程停止/启动,它会起作用。当我尝试启动已经处于“已启动”状态的进程时会出现问题。

用法:./abc.sh start/stop 12345

错误信息是:

HTTP 409: Conflict

org.apache.juneau.rest.RestException: Action START cannot be performed for application instance TechDev2 (fadc6b5c-782a-46a1-bf46-21f331f669c0) because its state is STARTED.
        at com.platform.vem.gui.web.rest.EgoRestCallHandler.renderError(EgoRestCallHandler.java:60)
        at com.ibm.asc.ascd.core.rest.api.ConductorRestCallHandler.renderError(ConductorRestCallHandler.java:79)
        at org.apache.juneau.rest.RestCallHandler.handleError(RestCallHandler.java:273)
        at org.apache.juneau.rest.RestCallHandler.service(RestCallHandler.java:189)

【问题讨论】:

    标签: shell unix sh


    【解决方案1】:

    您可以采取一些措施来提供帮助:

    在重新启动之前检查进程是否已经运行。

    使用ps -ef|grep <process>

    当您启动/停止进程时,使用$? 检索退出状态 并用它来设计流程。

    【讨论】:

    • 这不是一个 unix 进程,所以我无法 grep 它。我已经试过 $?两种情况下的输出都是 0
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-15
    • 1970-01-01
    • 1970-01-01
    • 2013-11-25
    • 1970-01-01
    • 2013-10-06
    • 1970-01-01
    相关资源
    最近更新 更多