Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
    
More than 80% of what? –  mgorven May 24 '12 at 20:08
add comment

2 Answers

up vote 0 down vote accepted

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.

share|improve this answer
    
Thanks you so much!! –  edotan May 24 '12 at 21:09
add comment

Assuming that the Java process has an initscript and a PID file, and you want to restart it when it uses more than 1G of RSS memory:

#!/bin/sh
set -e
RAM=`ps -o rss --no-headers -p $(cat /var/run/myservice.pid) || true`
if [ "$RAM" ]; then
    # It's not running
    service myservice start
elif [ "$RAM" -gt 1048576 ]; then
    # It's using too much memory
    service myservice restart
fi
share|improve this answer
    
Thanks you so much!! –  edotan May 24 '12 at 21:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.