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?Answer1:
Make sure your command signals success (with exit code 0) when it succeeds, and failure (non-zero) when it fails.
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).
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" fiAnswer2:
usually they shutdown with a signal, try to have some signal hander function to handle unpredictable shutdowns, or worst case have another monitoring process, like a task manager.
did you try anything?
by the way some signals cant be handled, like segmentation faults, <strong>SIGSEGV</strong>Answer3:
Simpler solution is
yourcommand if [ $? -eq 0 ] ; then echo "It exited successfully" else echo "It exited with failure, exitcode $?" fi