Skip to content

Instantly share code, notes, and snippets.

@zauberstuhl
Last active April 14, 2016 09:55
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 zauberstuhl/1455688388e5d818308dc88615439ff8 to your computer and use it in GitHub Desktop.
Save zauberstuhl/1455688388e5d818308dc88615439ff8 to your computer and use it in GitHub Desktop.
Sidekiq 3 Memory Killer Script - https://blog.zauberstuhl.de/sidekiq-3-memory-killer
#!/bin/bash
# check if script is already running
SELFCHECK=$(ps -eo pid,command |grep -v grep |grep $0 |wc -l)
if [ "$SELFCHECK" -gt 2 ]; then
echo "$0 is already running!";
exit 1;
fi;
NAPTIME=120
MAXRSS=$(( 4200 * 1024 )); # 4.2GB
FOUND=$(ps -eo rss,pid,command |grep sidekiq |grep -v grep |grep -v $0);
while read -r line; do
echo -n "$line ...";
USAGE=$(echo $line |cut -d' ' -f1);
PID=$(echo $line |cut -d' ' -f2);
if [ "$USAGE" -gt $MAXRSS ]; then
echo -e " \033[0;31mrestart\033[0m";
kill -SIGUSR1 $PID;
# wait before we forget about the graceful stuff
sleep $NAPTIME;
kill -SIGKILL $PID;
# prevent killing all worker at once
sleep $(( $NAPTIME / 2 ));
else
echo -e " \033[0;32mok\033[0m";
fi;
done <<< "$FOUND"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment