The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Throttling Process Resource Consumption humbletech99 UNIX for Advanced & Expert Users 3 03-03-2009 07:13 AM
real memory consumption of a process melanie_pfefer SUN Solaris 4 11-18-2008 06:12 PM
Ghost Process + CPU consumption neal.opperman SCO 1 02-14-2008 04:53 AM
PS command does not display shell script running hadarot Shell Programming and Scripting 4 09-15-2005 06:56 AM
Shell Script Display? wmosley2 UNIX for Dummies Questions & Answers 2 12-14-2003 02:12 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-26-2009
mr_awd mr_awd is offline
Registered User
  
 

Join Date: May 2009
Posts: 2
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 (permalink)  
Old 05-26-2009
vbe's Avatar
vbe vbe is offline Forum Staff  
Moderator
  
 

Join Date: Sep 2005
Location: Switzerland - GE
Posts: 1,585
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 (permalink)  
Old 05-26-2009
mr_awd mr_awd is offline
Registered User
  
 

Join Date: May 2009
Posts: 2
Quote:
Originally Posted by vbe View Post
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:58 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0