LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-09-2012, 03:54 AM   #1
Toushi
LQ Newbie
 
Registered: Apr 2011
Posts: 18

Rep: Reputation: 0
Bash Scripting -- Find High utilize process.


Purpose: Collect the resource utilization information and give the information about owner whos process is utilizing more cpu.

Code:
#!/bin/bash

clear

LOADAVG=`top -n1 |head -1 | awk  '{print $10, $11, $12, $13, $14}'`
CPUUSAGE=`top -n1 |head -10`
MEMINFO=`free -m`
IODETAIL=`vmstat -n`
USERS=`top -n1 | tr '\n' ' ' | awk '{print $70}'`
PROCID=`top -n1 | tr '\n' ' ' | awk '{print $69}'`
PROCEDETAIL=`ps -ef | grep -F  $PROCID`
USERNAME=`phone $USERS | awk '{print $1}' |cut -f 2  -d ':'`
EMAILID=`phone $USERS | awk '{print $2}' |cut -f 2 -d ' ' - |cut -f3  -d ':' -`

echo
echo "Current CPU utilization is:"
echo "--------------------------------------------------"
echo "$CPUUSAGE"
echo
echo "Current load on the system is:"
echo "--------------------------------------------------"
echo "$LOADAVG"
echo
echo "Current memory utilization on the system is:"
echo "--------------------------------------------------"
echo "$MEMINFO"
echo
echo "Current system I/O detail:"
echo "--------------------------------------------------"
echo "$IODETAIL"
 if [ $USERS != "root" ] &&  [ $USERS  != $USER ]
   then
  echo "Current high cpu utilize process deatil is:"
  echo "--------------------------------------------------"
  echo "$PROCEDETAIL"
  echo
  echo "User detail has been given below:"
  echo "--------------------------------------------------"
  echo "User name is: $USERNAME"
  echo "User Email id: $EMAILID"
  echo
   else
 echo
 echo "The process is belongs to root or no other user utilizing high cpu on the server"
 echo "please check other resource utilization."
 fi

###END####
Problem:
In this script I am getting two problem.
1) Every time the top command out put getting different out put. In following variable I m looking for user id in top process.
Code:
 USERS=`top -n1 | tr '\n' ' ' | awk '{print $70}'`
$70 give me different value some time because of that my script getting failed.

2)
Code:
PROCEDETAIL=`ps -ef | grep -F  $PROCID`
Above command not getting execute successfully and also same getting wrong value in $PROCID. It should take PID of top process.

Note:- phone is ldap command to check user detail. Example of output.
phone abc58208
abc58208:Toushi Patil :Contractor:toushi.patil@xyz.com:Banglore:BLRO Karnataka

Filter output should be look like:

User name: Toushi Patil
Email Id: tousshi.patil@xyz.com

Please help in this issue.

Last edited by Toushi; 08-09-2012 at 09:32 AM.
 
Old 08-09-2012, 04:22 PM   #2
cbtshare
Member
 
Registered: Jul 2009
Posts: 645

Rep: Reputation: 42
to get the user try

Quote:
top -n1 | grep -vE 'USER' | cut -d " " -f4
 
Old 08-09-2012, 07:28 PM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,348

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
This seems more robust to me
Code:
# Given
top -n1 | grep -A1 USER
  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
15135 root      25   0 12100  744  636 R   99  0.0  21090:58 hplog


# Then extending, using bash arrays
arr=($(top -n1 | grep -A1 USER|grep -v USER|cut -d' ' -f1-2))
echo ${arr[0]}
15135
echo ${arr[1]}
root
 
1 members found this post helpful.
Old 08-09-2012, 08:15 PM   #4
cbtshare
Member
 
Registered: Jul 2009
Posts: 645

Rep: Reputation: 42
ahhh
Quote:
grep -A1
is a dream! glad I learned something today ..thx chris!..My previous solution was terrible lol

This will work even though a bit tacky

Quote:
top -n1 | grep -A1 USER | awk '{print $3}'| head -n2 | tail -n1
 
Old 08-09-2012, 08:42 PM   #5
okcomputer44
Member
 
Registered: Jun 2008
Location: /home/laz
Distribution: CentOS/Debian
Posts: 246

Rep: Reputation: 53
Quote:
Originally Posted by cbtshare View Post
ahhh is a dream!
Yes it is!
I have just checked what it does and I'm amazed too.
 
Old 08-10-2012, 01:20 AM   #6
Toushi
LQ Newbie
 
Registered: Apr 2011
Posts: 18

Original Poster
Rep: Reputation: 0
Dear Chris & cbtshare,

Thanks you very much to provide me the solution. Chris your solution is applicable to
my script. Both are you great

I have one more problem in my script.
When I grep the process with ps -ef command it not give me proper output.
Current command I am using.
#Variable:

arr1=($(ps -ef |grep $PROCID))
PROCEDETAIL=${arr1[0]}

echo "Current high cpu utilize process deatil is:"
echo "--------------------------------------------------"
echo "$PROCEDETAIL"
 
Old 08-10-2012, 01:49 AM   #7
cbtshare
Member
 
Registered: Jul 2009
Posts: 645

Rep: Reputation: 42
simply use
Quote:
PROCID=$(top -n1 | grep -A1 USER | awk '{print $2}'| head -n2 | tail -n1)
 
Old 08-12-2012, 06:10 AM   #8
Toushi
LQ Newbie
 
Registered: Apr 2011
Posts: 18

Original Poster
Rep: Reputation: 0
Hello cbtshare/All,

Thanks for reply!
Actually I am looking for detail of that PID store in PROCID variable using ps -ef |grep PID. When I try to invoke that variable in my script like.

Code:
PROCDETAIL=ps -ef |grep $PROCID
It store PID only in PROCDETAIL variable and if I try this different way it give me error mismatch argument.

Last edited by Toushi; 08-12-2012 at 06:11 AM.
 
Old 08-12-2012, 07:47 AM   #9
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Your script could be improved in several ways. Here's three.

Quote:
Originally Posted by Toushi View Post
1) Every time the top command out put getting different out put.
Most importantly: use temporary files when you need to (and you do).
Code:
nice -n -20 $$; export TMPDIR=/dev/shm
_MYTMPDIR=`mktemp -p /dev/shm -d progn.XXXXXXXXXX` && {
 generate_output0 > "${_MYTMPDIR}/somefile0" &
 generate_output1 > "${_MYTMPDIR}/somefile1" &
 wait_for_exit; process_output; rm -rf "${_MYTMPDIR}"
Storing data this way, having all commands produce output at almost the same time, ensures you get all information for that time "snapshot".


Quote:
Originally Posted by Toushi View Post
echo "Current CPU utilization is:"
Secondly prioritize. Separate "must haves" from "nice to have". Getting accurate data is, easier correlation is but pretty-printing is not the most important. And when you're pretty-printing why make it take up so much gawddarn space? The whole
Code:
echo
echo "Current CPU utilization is:"
echo "--------------------------------------------------"
echo "$CPUUSAGE"
echo
and more could be efficiently written as
Code:
echo -en "CPU Loadavg mem IO\n${CPUUSAGE} ${LOADAVG} ${MEMINFO} ${IODETAIL}\n"

Quote:
Originally Posted by Toushi View Post
LOADAVG=`top -n1 |head -1 | awk '{print $10, $11, $12, $13, $14}'`
Userland tool output can differ due to OS and application versions (unless portability isn't an issue) and can be influenced in several ways: environment variables, resource files and command line switches. So apart from some making the case for setting default behaviour always ("LANG=C; LC_ALL=C; export LANG LC_ALL") you should get to know your tools. Unless you configure 'top' using (a temporary!) ~/.toprc you'll be provided with more and varying information than you need resulting in longer execution time, a longer and potentially more difficult processing chain meaning more tool execution time. And you don't need 'top' in all cases. For instance returning all UID's could be as easy as
Code:
/bin/ps ax -o uid --noheaders
and returning all processes and only displaying actual CPU utilization and UID's could be done with
Code:
/bin/ps ax -eo pcpu,uid --noheaders|grep -v '^[[:blank:]]0.0'
(Note any conversion takes up time so while having local user names sounds nice it comes at a cost. Easier to 'getent passwd $UID' when you actually need it.)
I know, all of this sounds a bit like it's "too much" for a simple script but IMHO it's the thought process that counts. If you don't dig that just wait until you need to add details, run your script say once a second on a burdened machine or have somebody ask you to provide a portable version.

*One more thing. For some reinventing the wheel is compulsory, for others compulsive and for others completely unnecessary because lots of solutions are already available from Sourceforge, The-Site-Formerly-Known_As-Freshmeat, Nongnu, Berlioz, your distributions or 3rd party documentation, forum and web log posts. Sometimes being able to use somebody elses work or adapt something may be more efficient.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash Scripting -- Find big files and gzip it Toushi Linux - General 3 08-01-2012 02:44 AM
scripting, perl or bash; run a background process, get pid 2ck Programming 6 04-02-2010 12:16 AM
scripting a find, cp and bzip2 then scp process onewhoknows Linux - Newbie 2 06-08-2004 06:05 AM
Bash Scripting - child process affecting parent process mthaddon Linux - General 1 05-02-2004 01:19 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration