Kill top 5 memory uses process

Hi All,

how to kill 5 top memory used process in my hp-ux.

Thanks,
Kki

What have you tried so far? Are you sure you want to kill the processes, even if it is the main application on that server?

i can get top 5 memories processes using ps aux command, but i want to know is there any command or shell script for this to kill after finding top memories from server.

You best bet is rebooting the server. Randomly killing processes is not a sane approach.

If you are running a database or a web server, then either or both are likely to be in the top 5 processes for memory. You may want to look at how much memory is allocated by the applications on your server and shrink the memory pool of those applications. You would likely need to restart the applications. Hence you should probably get downtime.

If you kill the wrong process you will probably need a reboot, and if you don't change the memory allocation, you will still be short on memory after the reboot.

Of course there is, albeit i have to give the same warnings jiliagre and zaxxon already issued: don't do it, because this is not a sound procedure.

Look at the output of ps aux :

USER          PID %CPU %MEM   SZ  RSS    TTY STAT    STIME  TIME COMMAND
root       131076 12.0  0.0  448  448      - A      Jan 26 101800:17 wait
root      1048608 11.8  0.0  448  448      - A      Jan 26 99941:41 wait
root       983070 10.7  0.0  448  448      - A      Jan 26 90791:37 wait
root      1114146 10.7  0.0  448  448      - A      Jan 26 90765:00 wait
root      4587534  1.4  2.0 134488 134712      - A      Jan 26 11558:45 storstpd start
root     10158134  0.3  1.0 65976 66516      - A      Jan 26 2477:26 storapid st

You need the second column (the PID) for the kill command. Further, you need to disregard the first line because it contains headers rather than data:

ps aux | <some filter> ... |\
while read junk PID junk ; do
     echo kill -15 $PID
done

Your ps-output may differ slightly from mine (i took mine from an AIX system, not having a HP-Ux system at hand) and you might have to adjust the command slightly. There might be processes which do not respond to signal 15 (this would indicate poor programming) so you might need to change kill -15 <pid> to kill -9 <pid> . If you do so, programs will not be able to clean up after them: shared memory segments, FIFOs and the like as well as temporary files with be left over.

Test thoroughly before you remove the "echo"-statement. Even with thorough testing i suggest to never use this.

I hope this helps.

bakunin

On my HP-UX, ps aux gives the same output as ps blabla and ps .

On SuSE Linux this is built into the kernel; they call it OOM killer. I call it amok runner.

I agree that randomly killing the top 5 "user" memory processes is not sensible.

On HP-UX you need to work from ps -al .

Assuming ksh .

To get you started. Execute the "ps" command, exclude the heading line containing "PID", sort numerical reversed by size, extract the top 5, extract the fields for PID and SIZE.

ps -al | grep -v "PID" | sort +9 -r | head -5 | awk '{print $4,$10}' | while read PID SIZE
do
    echo "${PID} ${SIZE}"
done

If the SIZE figures seem small it is because it is in 4k pages.

(On some HP-UX versions the Berkeley unix syntax ps aux is available by invoking UNIX95 mode within the Shell).

ps -al excludes process group leaders, while ps -el lists all processes.

ps -el | tail +2 | sort -k 10nr | awk '{print $4,$10} NR==5{exit}'

On HP-UX the UNIX95 environment variable enables Posix mode (not BSD mode)
Posix has the nice -o format option:

UNIX95=1 ps -e -o pid= -o sz= | sort -k 2nr | head -5

can u check this for process pid listing

ps -eo %mem,pid  | sort -n -k 1  | grep -iv mem | tail -5 | awk '{ print $1 }'