本文介绍了当检测进程退出或者被杀死由于内存不足的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的bash脚本运行在后台并等待指令等待它停止一些程序。但是有一个高的可能性,即后台进程就会被杀死,因为它需要太多内存。我希望我的脚本来为最终轻轻一杀一个过程有不同的反应。如何检查这个条件?

My bash script is running some program in background and with wait command waits for it to stop. But there is a high possibility that the background process will be killed because it takes too much memory. I want my script to react differently for a process that ended up gently and for a killed one. How do I check this condition?

推荐答案

请确保您的命令信号的成功(与出口code 0),当它成功,和失败(不为零),当它失败。

Make sure your command signals success (with exit code 0) when it succeeds, and failure (non-zero) when it fails.

当一个进程被OOM杀手用SIGKILL杀死,信号故障是自动的。 (外壳会考虑信号的退出code终止的进程是128 +信号数,所以128 + 9 = 137 SIGKILL)。

When a process is killed with SIGKILL by the OOM killer, signaling failure is automatic. (The shell will consider the exit code of signal terminated processes to be 128 + the signal number, so 128+9=137 for SIGKILL).

您然后使用一个事实,即等待somepid 与同code,因为它在if语句上等待命令退出:

You then use the fact that wait somepid exits with the same code as the command it waits on in an if statement:

yourcommand &
pid=$!
....
if wait $pid
then
    echo "It exited successfully"
else
    echo "It exited with failure"
fi

这篇关于当检测进程退出或者被杀死由于内存不足的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 16:49
查看更多