崩溃时退出代码 [英] exit code when crashed

查看:29
本文介绍了崩溃时退出代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过 shell 脚本了解应用程序是否崩溃.如果应用程序崩溃,退出代码是什么?

I like to know whether application is crashed or not from shell script. What would be the exit code if application crashed?

推荐答案

应用程序的退出代码将在 shell 变量 $? 中.如果您的应用程序崩溃,即操作系统认为它做了一些坏事,并导致它终止(向它发送一个信号),那么这会反映在退出状态 $? 中.

The exit code of the application will be in the shell variable $?. If your application crashes, i.e. the operating system decides it has done something bad, and causes it to terminate (sends it a signal), then this is reflected in exit status $?.

这是我使用的一个简单函数(在 bash 中我将它设置为 PROMPT_COMMAND 变量) put 对退出状态进行一些解码

Here is a simple function I use (in bash I set it as the PROMPT_COMMAND variable) put does some decoding of the exit status

check_exit_status () 
{ 
    local status="$?";
    local msg="";
    local signal="";
    if [ ${status} -ne 0 ]; then
        if [ $((${status} < 128)) -ne 0 ]; then
            msg="exit (${status})";
        else
            signal="$(builtin kill -l $((${status} - 128)) 2>/dev/null)";
            if [ "$signal" ]; then
                msg="kill -$signal$msg";
            fi;
        fi;
        echo "[${status} => ${msg}]" 1>&2;
    fi;
    return 0
}

希望你觉得它有用.

这篇关于崩溃时退出代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆