Cron job and shell script to kill a process if memory gets to high

Hello,

I'd like to set a cron job that runs a shell script every 30 minutes or so to restart a java based service if the memory gets above 80%. Any advice on how to do this?

Thanks in advance!

  • Ryan

which OS ?

Why not limit the amount of memory java can use instead?

I fully agree with Corona688. It's what the unix ulimit command is for. Well written code should respect the limits. Bad code will crash. Try this on a test system.

Wish I could, however, the application in question, Web Help Desk, is a memory hog.

---------- Post updated at 04:28 PM ---------- Previous update was at 04:28 PM ----------

I'm using CentOS 5.8

try this

if [ `free -m | grep Mem | awk '$3/$2 > .8'`  != "" ];then
<restart your service>
fi

Wow, this looks great. Thanks so much!

In the != "", can I assume that I enter !="java"? java is actually the name of the process that's taking up 80% or more of memory.

do you specifically want to monitor java memory consumption for 80% ? or if at any point system memory usage shoots over 80% then restart java, irrespective of how much memory java is consuming

It's really java that is causing the Nagios alerts. Java eventually creeps up to 100% of it's allotted utilization.

***** Nagios *****

Notification Type: PROBLEM

Memory WARNING - 82.9% (857524 kB) used

try this instead

if [ `ps aux | grep java | sort -r -k4 | head -1 | awk '{print $4}'` > 80 ];then
<restart service>
fi

Thanks so much. I'll definitely give that a try.

Didn't seem to work. Restarted the daemon regardless of memory usage. Didn't see any output as to why.

Here's what I put in shell script:

#!/bin/sh

#  Restart WHD if memory usage hits 80% or higher
#  

if [ `ps aux | grep java | sort -r -k4 | head -1 | awk '{print $4}'` > 80 ];then
/usr/local/webhelpdesk/whd restart
fi
exit 0

The > 80 will create a file called 80 (check your directory).

Have you tried:

#!/bin/sh

#  Restart WHD if memory usage hits 80% or higher
#  

if [ `ps aux | grep java | grep -v "grep" | sort -r -k4 | head -1 | awk '{print $4}'` -gt 80 ];then
/usr/local/webhelpdesk/whd restart
fi
exit 0

Beware that a process exceeding 80% memory (assuming that this is what your version of ps displays in field 4? ... very unlikely?) on a spot check is nothing unusual unless your system has a memory issue.

Please post your Operating System and version and the Shell which you are using along with a representative sample of ps -aux|grep "java"|grep -v "grep" , complete with column headings (extracted from ps -aux ). There is much variation in the ps command.

1 Like

my bad, @methyl you are right

I'm running CentOS release 5.8 (Final)

The

ps aux | grep java | sort -r -k4 | head -1 | awk '{print $4}'

command does list the amount of memory Java is using. When I run that command it's listed right now at 65.7. In true fashion, at 2:46 PM CST it's on it's way to exceeding 80% soon. I really don't think this VM has a memory issue and really believe it's the app itself.

I tried the new command

if [ `ps aux | grep java | grep -v "grep" | sort -r -k4 | head -1 | awk '{print $4}'` -gt 80 ];then
/usr/local/webhelpdesk/whd restart

and it returned:

monitor.sh: line 9: [: 66.3: integer expression expected

I think this means it's working because I've run it several times in the last 40 minutes and the number is increasing each time.

Try:

awk '{print int($4)}'
1 Like

That did it. So now I've got:

if [ `ps aux | grep java | sort -r -k4 | head -1 | awk '{print int($4)}'` -gt 80 ];then
  /usr/local/webhelpdesk/whd restart
fi

exit 0

Thanks!

1 Like

Might be better as a numeric sort (though the decimal point in the sample data is likely to confuse):

Other posters may wish to comment on how to sort numeric fields with a decimal point in CentOS release 5.8 (Final) .

Thanks none-the-less. I really appreciate everyone's help!

FWIW, you could try a variation (no sort needed)

cpu_low() {
  ps aux | awk '$0~p && $4 > b {exit 1}' p="$1" b="$2"
}

if ! cpu_low java 80 ; then
  /usr/local/webhelpdesk/whd restart
fi
1 Like