This is a monit recipe for a rails app with 4 Unicorn threads using SOCKET. The app averages a total memory usage of 600 MB.
If it goes over 800 MB send email alert only.
If over 860 MB, reload the app.
Also if it’s using too much CPU, alert by email.
I use unicorn_rails on socket mode with nginx, and preload_app = true.
check process myapp_unicorn with pidfile /var/www/apps/my_app.com/shared/pids/unicorn.pid group unicorn if totalmem > 800 MB for 2 cycles then alert if totalmem > 860 MB for 2 cycles then exec "kill -HUP `cat /var/www/apps/my_app.com/shared/pids/unicorn.pid`" if cpu > 40% for 2 cycles then alert if cpu > 20% for 4 cycles then alert if failed unixsocket /var/www/apps/my_app.com/shared/pids/unicorn.sock then alert
note: the HUP signal will reload the children processes without reloading code changes. This is fast and desirable.