Skip to content

Instantly share code, notes, and snippets.

@eusonlito
Created March 26, 2024 08:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eusonlito/f2dc01f7738c84370face160244879de to your computer and use it in GitHub Desktop.
Save eusonlito/f2dc01f7738c84370face160244879de to your computer and use it in GitHub Desktop.
This script identifies the second most memory-intensive PHP process, resets the Out-Of-Memory (OOM) priority of the previously adjusted process if still running, sets the current process's OOM priority to be more likely killed, and logs its PID and timestamp.
#!/bin/bash
APP="php"
CURRENT_HIGHEST=$(ps -eo pid,comm --sort=-%mem | grep "$APP" | head -n 2 | tail -n 1)
CURRENT_HIGHEST_PID=$(echo $CURRENT_HIGHEST | awk '{print $1'})
LAST_PID_FILE="/tmp/oom-kill-previous"
if [ -f "$LAST_PID_FILE" ]; then
LAST_PID=$(cat $LAST_PID_FILE)
if [ "$LAST_PID" != "" ] && [ -d "/proc/$LAST_PID" ]; then
echo 0 > /proc/$LAST_PID/oom_score_adj
fi
fi
echo 1000 > /proc/$CURRENT_HIGHEST_PID/oom_score_adj
echo $CURRENT_HIGHEST_PID > $LAST_PID_FILE
echo "[$(date --iso-8601=seconds)] $CURRENT_HIGHEST"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment