Skip to content

Instantly share code, notes, and snippets.

@mansurali901
Last active June 6, 2018 11:41
Show Gist options
  • Save mansurali901/27c91ac31e6376f0ac62dd5526eb94c7 to your computer and use it in GitHub Desktop.
Save mansurali901/27c91ac31e6376f0ac62dd5526eb94c7 to your computer and use it in GitHub Desktop.
Adjust value of OOM score to avoid process killed by kernel in case of Out of Memory
#!/bin/bash
# This script is used to disable OOM Killer for Ruby
# Process script will find the ruby process pid and change
# the OOM value to avoid being killed by kernel
# Author : Mansur Ul Hasan
# Email : [email protected]
# Function to find the PID
ProcessName='ruby' # Process Name
AppDir='/usr/local/script/oomadjust' # Application path
PidFinder () {
# Checking PID for process defined in {ProcessName} variable
pgrep -f ruby |grep -v "grep\|pgrep" > $AppDir/conf/ruby.pid
# Changing oom value to safe the process state in OOM situation
for pid in `cat $AppDir/conf/ruby.pid`
do
## Variable to check the process name with the help of $pid
pidvalue=`cat /proc/$pid/status |grep Name |awk '{print $2}'`
## Checking if process is really the process we looking to optimize
case $pidvalue in
*ruby*)
# If process is valid value for oom_score_adj will be tune to lower value
echo "Process is valid $pidvalue"
echo -10 > /proc/$pid/oom_score_adj
;;
*)
# if process is not valid event will not be logged.
echo $pidvalue
echo "Process is not valid ..."
;;
esac
done
}
# Main function caller
PidFinder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment