Linux script – find java process and restart it if it is more than 80% Mem High

bashcentosjavalinuxscripting

I want to put in crontab a shell script that will monitor the java process and if it's the memory is more than 80% then I want to restart it.
can any one help?
Thanks!!
Elad.

Best Answer

Perfect use case for monit. http://mmonit.com/

Example Config

check process foo with pidfile "/var/run/foo"
start program = "/bin/foo -c foo.conf"
stop program = "/bin/kill -KILL
cat /var/run/foo.pid"
if totalmem is greater than X.0 MB for 5 cycles then restart

The reason you want monit rather than a script is that it can email you, provide logs, and honestly a lot more, but I'll leave the research up to you.