Skip to content

Instantly share code, notes, and snippets.

@matsubo
Last active June 20, 2019 09:54
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 matsubo/c47b8a60fc8557c1012545f8df752012 to your computer and use it in GitHub Desktop.
Save matsubo/c47b8a60fc8557c1012545f8df752012 to your computer and use it in GitHub Desktop.
*/15 * * * * /path/to/restart_if_high_memory.sh >> /tmp/restart_if_high_memory.log 2>&1
#!/bin/bash -x
set -e
HOME=/path/to/home
cd $HOME
PATH="$HOME/.rbenv/shims:$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
# returns 1 if usage is higher than 80%
usage=`free -m | sed -n 2p | awk '{ printf("%d", ($3/$2)/0.8) }'`
if [ $usage -lt 1 ]; then
exit 0
fi
cd path/to/current
# sidekiq process can be multiple so find process id and kill then start corresponding to the process.
list=`ps axu|grep sidekiq|grep -v grep|awk '{print $2}'`
for pid in $list; do
kill -TSTP $pid
sleep 10
kill -TERM $file`
sleep 10
RAILS_ENV=production bundle exec sidekiq -d
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment