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


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?

Best Answer

Perfect use case for monit.

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/"
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.