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
# 15  
Old 04-09-2012
I'm running CentOS release 5.8 (Final)

The
Code:
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
Code:
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:
Code:
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.

Moderator's Comments:
Mod Comment Please use code tags. Video tutorial on how to use them

Last edited by Scrutinizer; 04-09-2012 at 05:12 PM..
# 16  
Old 04-09-2012
Try:
Code:
awk '{print int($4)}'

This User Gave Thanks to Scrutinizer For This Post:
# 17  
Old 04-09-2012
That did it. So now I've got:

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

Last edited by Scrutinizer; 04-09-2012 at 05:19 PM.. Reason: Code tags
This User Gave Thanks to prometheon123 For This Post:
# 18  
Old 04-09-2012
Quote:
sort -r -k4
Might be better as a numeric sort (though the decimal point in the sample data is likely to confuse):
Quote:
sort -n -r -k4
Other posters may wish to comment on how to sort numeric fields with a decimal point in CentOS release 5.8 (Final).
# 19  
Old 04-09-2012
Thanks none-the-less. I really appreciate everyone's help!
# 20  
Old 04-09-2012
FWIW, you could try a variation (no sort needed)

Code:
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

This User Gave Thanks to Scrutinizer For This Post:
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