Last active
June 20, 2019 09:54
-
-
Save matsubo/c47b8a60fc8557c1012545f8df752012 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*/15 * * * * /path/to/restart_if_high_memory.sh >> /tmp/restart_if_high_memory.log 2>&1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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