Home > Linux > Unix shell scripts – List Processes based on %CPU and Memory Usage

Unix shell scripts – List Processes based on %CPU and Memory Usage

August 18th, 2010 Leave a comment Go to comments

Bash scripts berikut digunakan untuk mengetahui daftar proses dalam CPU dan penggunaan memori

$ pico processes.sh
#! /bin/bash

echo "Start Time" `date`
# By default, it display the list of processes based on the cpu and memory usage #
if [ $# -eq 0 ]
then

	echo "List of processes based on the %cpu Usage"
	ps -e -o pcpu,cpu,nice,state,cputime,args --sort pcpu  # sorted based on %cpu
	echo "List of processes based on the memory Usage"
	ps -e -orss=,args= | sort -b -k1,1n # sorted bases rss value

# If arguements are given (mem/cpu)
else
	case "$1" in
	mem)
	 echo "List of processes based on the memory Usage"
 	 ps -e -orss=,args= | sort -b -k1,1n
	 ;;
 	cpu)
	 echo "List of processes based on the %cpu Usage"
	 ps -e -o pcpu,cpu,nice,state,cputime,args --sort pcpu
	 ;;
 	*)
		echo "Invalid Argument Given \n"
		echo "Usage : $0 mem/cpu"
		exit 1
 	esac	

fi
echo "End Time" `date`
exit 0

Sebelum menjalankan scripts tersebut, patikan Anda mempunyai hak akses exceute (chmod +x). Cara menjalankan script tersebut sebagai berikut:

$ ./processes.sh

$ ./processes.sh mem

$ ./processes.sh cpu

Categories: Linux Tags:
  1. No comments yet.
  1. No trackbacks yet.

*

Bad Behavior has blocked 80 access attempts in the last 7 days.