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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Unix Script to find and kill a process with high memory utilization
# 1  
Old 03-05-2009
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 kill the process

top -n 1 -u user | grep ABC | awk '{print $1" "$10}' > pid_mem

#Example pid_mem where $1 is process id and $10 is %memory consumed
# file contains the pid and memory % as below example
12258 21.6
12298 51.5
19207 107.5
#read the text for file and determine the pid for process with %mem > 100
# example pid = 19207
#if exist # then iterate for sometime for the same process id to determine if the memory spike comes down for example

count=0
while (count <=5) {
top -n 1 -u user | grep $pid
if ($10 > 100)
sleep 10
if (count =5)
then if ($10 >100)
kill -9 $pid
echo "Memory is more than 100 so process killed" | mailx -r %date -s 'memory > 90%' abc@z.com
endif
endif
count =count + 1
endif
}
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. HP-UX

HP-UX server showing high memory utilization

Hi All, I am using HP-UX 11i v1(B11.11) servers for my work and its memory (RAM) utilization is consistenly 80% for the last one year. Though I am not facing any issues with this high memory utilization I just want to know the below queries: 1) Is it the default behavior of HP-UX systems? 2)... (20 Replies)
Discussion started by: ssk250
20 Replies

2. Solaris

High Memory Utilization

Hello Guys, I have one Solaris server with high memory utilization >90%. As per checking, below is the output for memory usage. bash-3.00# ps -efo pmem,uid,pid,ppid,pcpu,comm | sort -r %MEM UID PID PPID %CPU COMMAND 1.7 29496 20668 1 0.0 /opt/app/iw-home/tools/java/bin/java 1.5... (1 Reply)
Discussion started by: myrpthidesis
1 Replies

3. 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

4. Emergency UNIX and Linux Support

CPU and memory utilization of a process, by process name

Can someone please help me with a script that will help in identifying the CPU & memory usage by a process name, rather than a process id.This is to primarily analyze the consumption of resources, for performance tweaking. G (4 Replies)
Discussion started by: ggayathri
4 Replies

5. 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

6. Shell Programming and Scripting

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 (19 Replies)
Discussion started by: prometheon123
19 Replies

7. UNIX for Dummies Questions & Answers

Get CPU,Memory utilization by process id

Hi , We need to get the CPU% and Memory utilization of process by process id. Is there any way to do get them ? I tried few commands like top -p <PID> , but am getting error "Quitting top: pset <PID> doesn't exist" also i tried with ps -eo option but am getting error "ps: illegal option --... (1 Reply)
Discussion started by: suresh_g
1 Replies

8. Shell Programming and Scripting

script to check high cpu utilization for java process

Hello Team, I need help in preparing script to check for high cpu utilisation for java process. I have many java process on my system which consumes high cpu so i have to monitor it using script. ---------- Post updated 12-10-10 at 02:21 AM ---------- Previous update was 12-09-10 at... (1 Reply)
Discussion started by: coolguyamy
1 Replies

9. AIX

High CPU utilization by a pro*C process

Hi , we upgarded our AIX from 5.3 to 6.1 and upgraded our xlc compiler from ver 6.0 to 9.0 . After this upgrade one of our pro*C program is utilizing around 20% of the CPU. Before upgarde its using only 0.2 %. when i try to debug using the truss command i got the below error. $... (6 Replies)
Discussion started by: mugunthanvh
6 Replies

10. 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
Login or Register to Ask a Question
rc(1M)																	    rc(1M)

NAME
rc - general purpose sequencer invoked upon entering new run level SYNOPSIS
DESCRIPTION
The shell script is the general sequencer invoked upon entering a new run level via the command (where N equals 0-6). The script is typi- cally invoked by the corresponding entry in the file as follows: is the startup and shutdown sequencer script. There is only one sequencer script and it handles all of the sequencer directories. This script sequences the scripts in the appropriate sequencer directories in alphabetical order as defined by the shell and invokes them as either startup or kill scripts. If a transition from a lower to a higher run level (i.e., init state) occurs, the start scripts for the new run level and all intermediate levels between the old and new level are executed. If a transition from a higher to a lower run level occurs, the kill scripts for the new run level and all intermediate levels between the old and new level are executed. If a start script link (e.g., in sequencer N has a stop action, the corresponding kill script should be placed in sequencer (e.g., Actions started in level N should be stopped in level This way, a system shutdown (e.g., transition from level 3 directly to level 0) will result in all subsystems being stopped. Start and Kill Scripts In many cases, a startup script will have both a start and a kill action. For example, the inetd script starts the Internet daemon in the start case, and kills that process in the stop case. Instead of two separate scripts, only one exists, which accepts both the and argu- ments and executes the correct code. In some cases, only a start action will be applicable. If this is the case, and if the action is specified, the script should produce a usage message and exit with an error. In general, scripts should look at their arguments and pro- duce error messages if bad arguments are present. When a script executes properly, it must exit with a return value of zero. If an error condition exists, the return value must be nonzero. Naming Conventions The startup and shutdown scripts (referred to as startup scripts hereafter) exist in the directory, named after the subsystem they control. For example, the script controls starting up the daemon. The contents of sequencer directories consist of symbolic links to startup scripts in These symbolic links must follow a strict naming convention, as noted in the various fields of this example: where the fields are defined as follows: The sequencer directory is numbered to reflect the run level for which its contents will be executed. In this case, start scripts in this directory will be executed upon entering run level 2 from run level 1, and kill scripts will be executed upon entering run level 2 from run level 3. The first character of a sequencer link name determines whether the script is executed as a start script (if the character is or as a kill script (if the character is A three digit number is used for sequencing scripts within the sequencer directory. Scripts are executed by type (start or kill) in alphabetical order as defined by the shell. Although it is not recommended, two scripts may share the same sequence number. The name of the startup script follows the sequence number. The startup script name must be the same name as the script to which this sequencer entry is linked. In this exam- ple, the link points to Note that short file name systems require file names of 14 or less characters. This means that the fourth field is limited to 10 or fewer characters. Scripts are executed in alphabetical order. The entire file name of the script is used for alphabetical ordering purposes. When ordering start and kill script links, note that subsystems started in any given order should be stopped in the reverse order to eliminate any dependencies between subsystems. This means that kill scripts will generally not have the same numbers as their start script counterparts. For example, if two subsystems must be started in a given order due to dependencies (e.g., followed by the kill counterparts to these scripts must be numbered so that the subsystems are stopped in the opposite order in which they were started (e.g., followed by Also keep in mind that kill scripts for a start script in directory will reside in For example, and might be start/kill counterparts. Arguments The startup/shutdown scripts should be able to recognize the following four arguments (where applicable): The argument is passed to scripts whose names start with Upon receiving the argument, the script should perform its start actions. The argument is passed to scripts whose names start with Upon receiving the argument, the script should perform its stop actions. The argument is passed to scripts whose names start with so that the script can report back a short message indicating what the start action will do. For instance, when the spooler script is invoked with a argument, it echoes This string is used by the startup routines. Scripts given just the argument will only print a message and not per- form any actions. The argument is passed to scripts whose names start with so that the script can report back a short message indicating what the stop action will do. For instance, when the spooler script is invoked with a argument, it echoes This string is used by the shutdown checklist. Scripts given just the argument will only print a message and not perform any actions. Script Output To ensure proper reporting of startup events, startup scripts are required to comply with the following guidelines for script output. o Status messages, such as must be directed to stdout. All error messages must be directed to stderr. o Script output, both stdout and stderr, is redirected to log file unless the startup checklist mode is set to the raw mode. In this case, all output goes to the console. All error messages should be echoed to stdout or stderr. o Startup scripts are not allowed to send messages directly to the console, or to start any daemons that immediately write to the console. This restriction exists because these scripts are now started by the checklist wrapper. All script output should go to either stdout or stderr, and thus be captured in a log file. Any console output will be garbled. o When a startup script returns an exit code of can display a specific message on the console prior to rebooting the system. This is achieved by creating a text file named containing the text to be displayed to the console. Note that deletes this file after displaying the message, so startup scripts need to write this file each time a specific message is required to be displayed on console prior to reboot. RETURN VALUE
The return values for startup scripts are as follows: Script exited without error. Script encountered errors. Script was skipped due to overriding control variables from files, or for other reasons, and did not actually do anything. Script will automatically reboot the system. Script exited without error and started a process in background mode. For return values greater than the action is same as return value script encountered errors. SEE ALSO
init(1M), shutdown(1M), inittab(4), rc.config(4). rc(1M)