Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 0 down vote accepted

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.

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

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:

set -e
RAM=`ps -o rss --no-headers -p $(cat /var/run/ || 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
share|improve this answer
Thanks you so much!! – edotan May 24 '12 at 21:09

Your Answer


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.