38112

Detect when process quits or is being killed due out of memory

Question:

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" fi

Answer2:

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

Recommend

  • Django Graphene, Passing JSON or dict data as input for Mutation
  • Loading jQuery into Grunt.js using the grunt-contrib-jasmine plugin
  • IOS: automatic reference counting
  • Servlets 3.1: how to handle body in DELETE request
  • How do I handle options in a packaged Chrome App?
  • Storm latency caused by ack
  • How to get rid of the suffix on object names added by EF?
  • Pattern combining type test and literal
  • Behaviour of System.Timer when Interval property changed
  • How open new window with base64 PDF content?
  • why lock_guard can get an already locked mutex by unique_lock?
  • Extjs, handling success or failure when doing a standard submit in a form
  • Scrolling News Ticker Jquery - Issues
  • Recursion Control flow
  • How to implement simple validation in Scala
  • Does Context/Scoping of a SQLAlchemy Session Require Non-Automatic Object/Attribute Expiration?
  • Javascript unload page condition
  • How can I determine which routines MATLAB uses to solve a sparse matrix?
  • Meteor.. accounts- password— Create account on client without login
  • (Play 2.5) How do you define json format for type alias of an Option?
  • Is it safe to drop the -webkit vendor prefix from the css3 border-radius yet?
  • Ubuntu and bcrypt
  • nonblocking BIO_do_connect blocked when there is no internet connected
  • Using JRuby with Rails 3.2
  • Code in Job's Script Block after Start-Process Does not Execute
  • How to write order and limit within cakephp joins array
  • why do I get the error when installing the gem 'pg'? [duplicate]
  • How to avoid particles glitching together in an elastic particle collision simulator?
  • Get data from AJAX - How to
  • Recording logins for password protected directories
  • Splitting given String into two variables - php
  • Bug in WPF DataGrid
  • Obtain ObjectIdHex value from mgo query
  • Check if a string to interpolate provides expected placeholders
  • MySQL WHERE-condition in procedure ignored
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • java string with new operator and a literal