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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Cron job and shell script to kill a process if memory gets to high
# 8  
Old 04-05-2012
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
# 9  
Old 04-05-2012
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
# 10  
Old 04-05-2012
try this instead

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

# 11  
Old 04-06-2012
Thanks so much. I'll definitely give that a try.
# 12  
Old 04-09-2012
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:

Code:
#!/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


Last edited by methyl; 04-09-2012 at 03:55 PM.. Reason: please use code tags
# 13  
Old 04-09-2012
The > 80 will create a file called 80 (check your directory).

Have you tried:
Code:
#!/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.

Last edited by methyl; 04-09-2012 at 04:28 PM..
This User Gave Thanks to methyl For This Post:
# 14  
Old 04-09-2012
my bad, @methyl you are right

Last edited by 47shailesh; 04-09-2012 at 04:58 PM.. Reason: correction
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to monitor process with high CPU

Hi, Linux redhat 5.5 I need to write a kshell script that shows all the process that consume 100% CPU (or more. strange but there are time that top shows higger value that 100) and they are active more than 5 minute. The top command shows all the relevat information: The PID of the cpu ,... (0 Replies)
Discussion started by: yoavbe
0 Replies

2. Solaris

[DOUBT] Memory high in idle process on Solaris 10 (Memory Utilization > 90%)

Hi Experts, Our servers running Solaris 10 with SAP Application. The memory utilization always >90%, but the process on SAP is too less even nothing. Why memory utilization on solaris always looks high? I have statement about memory on solaris, is this true: Memory in solaris is used for... (4 Replies)
Discussion started by: edydsuranta
4 Replies

3. Shell Programming and Scripting

shell script to find a process by name and kill it

hi, Am a newbie to unix and wasnt able to write script to my requirement. I need a shell script, which should find a process by name and kill it. For eg: let the process name be "abc". I have different processes running by this name(abc), so should kill them all. Condition would be: if... (7 Replies)
Discussion started by: fop4658
7 Replies

4. Shell Programming and Scripting

shell script to kill process with respect to terminal

Hi, I've a script which kills all process, but i need a script shell script(sh), where it'll kill process on that particular terminal. below is example TY=`tty` for P in $TY do `kill -9 $P 2>/dev/null`; done echo "test process killed" break ... (3 Replies)
Discussion started by: asak
3 Replies

5. UNIX for Dummies Questions & Answers

cron job for the created shell script

Hi am newbie for unix shell.. how to create a cron job for my already created shell script.:confused: Thanks! (1 Reply)
Discussion started by: vidhyaS
1 Replies

6. Solaris

Shell Script gives error when run through cron job.

Hi, The following shell script runs without any problem when executed manulally. USED=$(df -h /arch | tail -1 | awk '{print $5}' | cut -d '%' -f 1) if then find /arch/AUBUAT/ -type f -mtime +0 | xargs rm find /arch/AUBMIG/ -type f -mtime +0 | xargs rm fi But the same gives below... (6 Replies)
Discussion started by: ksadiq79
6 Replies

7. Shell Programming and Scripting

Kill a process from parent shell within a shell script

Hi, I am looking for a solution for the following problem: Im Using tcpdump within a shellskript started in a subshell by using brackets: ( /usr/sbin/tcpdump -i ... -c 1 ) - I want the outout of tcpdump saved in a variable - Than tcpdump-Process in the Subshell should be killed - and I... (6 Replies)
Discussion started by: 2retti
6 Replies

8. Shell Programming and Scripting

Shell Script to Kill Process(number of process) Unix/Solaris

Hi Experts, we do have a shell script for Unix Solaris, which will kill all the process manullay, it used to work in my previous env, but now it is throwing this error.. could some one please help me to resolve it This is how we execute the script (and this is the requirement) ... (2 Replies)
Discussion started by: jonnyvic
2 Replies

9. Shell Programming and Scripting

Cron job shell script..

Hey Guys, i was trying out a shell script which has to remove a file for every 90 mins. this is the code i came up with . $ crontab -e file1 file1 contains 30 1 * * * * rm -r /folder1/folder2/somefile.txt Now i need the cron to run for every 90 mins. the problem with this is... (8 Replies)
Discussion started by: Irishboy24
8 Replies

10. Shell Programming and Scripting

Unix Script to find and kill a process with high memory utilization

Hi Unix Gurus i am somewhat new to unix scripting so need your help to create a script as below. # This script would find the process consuming memory beyond a certain #limit. if the meemory consumption is more than 100% for a period of 1 # minute for the specific process. the script would... (0 Replies)
Discussion started by: robinforlinux
0 Replies
Login or Register to Ask a Question