Shell script to display top 5 process (per cpu consumption)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to display top 5 process (per cpu consumption)
# 1  
Old 05-26-2009
Shell script to display top 5 process (per cpu consumption)

Hello,

I have to create shell script being myself real novice in scripting...
I was looking on the Internet for some script examples, but I don't have the basic understanding of the shell. I was trying to do something with “ps” command without success.
For exemple total number of running process: ps ax | wc -l | tr -d ""
Executed from command line work nice, but once in the script like echo ‘ps ax | wc -l | tr -d ""' return 1 which is not the right number of running process.

The script should do the following:

  • Should check the running processes
  • Display top five process (per cpu consumption)
  • output the summary for:
    • total number of processes
    • number of process per user
    • most common process
ex.: process1
process2
process3
process1
result : process1

Is it too difficult to implement in pure bash script?

Many thanks in advance for any help.
# 2  
Old 05-26-2009
Well, it depends a lot on what you are running...

On a HP server, using UNIX95 compliance, you would type to get the 5 most CPU consumer process type something like:
Code:
ant:/home/vbe $ UNIX95= ps -eo pcpu,pid,user,args | sort -r | head -5
15.95 12661 vbe      /opt/firefox/firefox-bin
 5.26 12694 vbe      /opt/Adobe/Acrobat7.0/Reader/hppahpux/bin/acroread --display 16
 1.24  2772 patrol   PatrolAgent
 1.10  2508 root     /opt/APPQcime/jre/bin/PA_RISC2.0/java -Djava.library.path=../li
 0.98 17873 root     /opt/perf/bin/midaemon

# 3  
Old 05-26-2009
Quote:
Originally Posted by vbe
Well, it depends a lot on what you are running...

On a HP server, using UNIX95 compliance, you would type to get the 5 most CPU consumer process type something like:
Code:
ant:/home/vbe $ UNIX95= ps -eo pcpu,pid,user,args | sort -r | head -5
15.95 12661 vbe      /opt/firefox/firefox-bin
 5.26 12694 vbe      /opt/Adobe/Acrobat7.0/Reader/hppahpux/bin/acroread --display 16
 1.24  2772 patrol   PatrolAgent
 1.10  2508 root     /opt/APPQcime/jre/bin/PA_RISC2.0/java -Djava.library.path=../li
 0.98 17873 root     /opt/perf/bin/midaemon

Thanks for your answer.
Server is running on ubuntu.
When I type the command in command prompt it format the output correctly. When executed from script like :
echo `UNIX95= ps -eo pcpu,pid,user,args | sort -r | head -5`
the output is formated in one line like this:
%CPU PID USER COMMAND 0.1 5162 root sshd: root@pts/1 0.0 7 root [khelper] 0.0 6 root [events/0] 0.0 5 root [watchdog/0]

Is there any formatting option or should'nt I use "echo"?

Do you have any idea for
  • number of process per user
  • most common process


Thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Top 5 cpu and Mem consuming process and files and suggestion for health check

I am middle of writing health check scripts, can you pls share commands on how I can get cpu and Mem of top consuming process info at the moment? Also can u suggest ideas on what all I can look for as a part do health check on red hat Linux server? I searched on site before posting, but... (2 Replies)
Discussion started by: Varja
2 Replies

2. UNIX for Beginners Questions & Answers

How to display only the first 5 running process using top in shell scripting?

topfunc() { top } topfunc Here i used the top command inside a function,and i called the function. when executing this bash file i get all the process which are using by the kernel i just want to display only the first 5 running process. is it possible? (7 Replies)
Discussion started by: Meeran Rizvi
7 Replies

3. Red Hat

Understanding cpu consumption with TOP

Hi please see attached picture of the TOP command. the "java" process seem to consume 700% of cpu is that mean that he uses 100% of every 7 cpu cores? thanks (2 Replies)
Discussion started by: guy3145
2 Replies

4. Shell Programming and Scripting

Shell script for logging cpu and memory usage of a Linux process

I am looking for a way to log and graphically display cpu and RAM usage of linux processes over time. Since I couldn't find a simple tool to so (I tried zabbix and munin but installation failed) I started writing a shell script to do so The script file parses the output of top command through... (2 Replies)
Discussion started by: andy_dufresne
2 Replies

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

6. UNIX for Advanced & Expert Users

Help! CPU consumption - %usr and %sys ??

On Linux, top shows how much % of CPU is consumed by "user" as well as "sys" like below. Tasks: 272 total, 3 running, 268 sleeping, 0 stopped, 1 zombie Cpu(s): 65.9%us, 33.8%sy, 0.0%ni, 0.2%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st Mem: 16300960k total, 16212488k used, 88472k free, ... (0 Replies)
Discussion started by: gomes1333
0 Replies

7. UNIX for Dummies Questions & Answers

how to get persistant cpu utilization values per process per cpu in linux (! top,ps)

hi, i want to know cpu utilizatiion per process per cpu..for single processor also if multicore in linux ..to use these values in shell script to kill processes exceeding cpu utilization.ps (pcpu) command does not give exact values..top does not give persistant values..psstat,vmstat..does njot... (3 Replies)
Discussion started by: pankajd
3 Replies

8. Shell Programming and Scripting

CPU Consumption comparision.

hi, i hav a script which processes 1000 files one by one in every run jus for searching their corresponding output files at a given path, due to this my script runs for long time and taking more CPU, can we have any way in which we can have this check at least 100 files in a single shot... (5 Replies)
Discussion started by: Prateek007
5 Replies

9. SCO

Ghost Process + CPU consumption

Hi everyone, I have a problem on sco unixware 7.1.4 a Ghost process is consuming cpu usage. rtpm cpu 0 ======--------- cpu 1 ===--------------------- cpu 2 ============================================================----------- cpu 3 ... (1 Reply)
Discussion started by: neal.opperman
1 Replies

10. UNIX for Advanced & Expert Users

CPU consumption

Hello experts, I am writing an application in Solaris, which is supposed to work on PrimePower 650 and SunFire 440 machines. The application listens to the events raised by the Hardware and OS in /dev/log. The application listens to the messages logged into /dev/log and do some processing on... (3 Replies)
Discussion started by: pleaseteachmeun
3 Replies
Login or Register to Ask a Question