Formatting the output of ps -eo command


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Old 07-30-2012
Formatting the output of ps -eo command

Hi ive been tasked to create a warning email which will be sent when a process exceeds a given percentage of CPU,
Ive created the script where it runs:
Code:
ps -eo pri,pid,user,nice,pcpu,comm  | awk '{if($5 >= 2)print $0}' >> /export/home/tjmoore/file2 2>/dev/null

I would then run a mail program to email it to me, This all works fine however I would like to format how it is displayed when it comes through on email,

This is currenlty what Im getting:
Code:
PRI   PID     USER    NICE    %CPU    COMMAND
 50 13109     root 20  3.0 _progres
  0  9838     root 20 10.8 _progres
 59  5790     root 20  4.1 _progres

Its looking a bit messy, can anyone help?

Last edited by Scott; 07-30-2012 at 09:43 AM.. Reason: Please use code tags
# 2  
Old 07-30-2012
Try setting the output field separator to, say, <TAB>:

Code:
awk 'OFS="\t" {$1=$1; if($5 >= 2) print}'

Don't forget to address some field (e.g. $1=$1) so $0 is recreated...

If that doesn't work try printf

Last edited by RudiC; 07-30-2012 at 10:07 AM..
# 3  
Old 07-30-2012
Given it ago but it doesnt seem to be working this is the script as it stands:

Code:
#!/bin/bash
#Send process which has exceeded 5%
#
#
echo 'The below is a list of processes which have exceeded the 5% threshold' >
/export/home/tjmoore/file2
#
echo ' ' >> /export/home/tjmoore/file2
#
echo 'PRI    PID     USER    NICE    %CPU    COMMAND' >> /export/home/tjmoore/fi
le2
#
if ps -eo pri,pid,user,nice,pcpu,comm | awk 'OFS="\t" {if($5 >= 2); $1=$1; print
 $0}' >> /export/home/tjmoore/file2 2>/dev/null
#
then /usr/local/bds/mailsend.s mailx "Process has surpassed 25% threshold" <email address> <email address> /export/home/tjmoore/file2 2&1>/dev/
null
fi
#

Moderator's Comments:
Mod Comment Please use code tags next time for your code and data.


---------- Post updated at 09:40 AM ---------- Previous update was at 09:17 AM ----------

sorry I am a novice at scripting, $1=$1 means argument 1 = argument 1?
What do you mean by:

Don't forget to address some field (e.g. $1=$1) so $0 is recreated...

Many Thanks for your help with this
# 4  
Old 07-30-2012
Sorry, I'm not sure I understand what you really want. The original question was on formatting; is that settled by now?
The script in your second post doesn't seem to work as expected - which might be phrased as "send a mail if that output files contains >=1 lines". If that's the case, we need awk to issue an exit code according to its success. As I'm not sure what intrinsic codes it sends, try this in your if statement:
Code:
ps ...  | awk  '{if($5>=2) {$1=$1; found=1; print}} END {exit !found}' OFS="\t"

And yes, $1=$1 means field 1 = field 1, which is a dummy assignment that causes $0 to be modified and thus recreated. If $0 is not recreated, the <TAB> OFS will not be inserted.
# 5  
Old 08-01-2012
Thankyou for your persistance with this

I dont seem to be getting any email through anymore so I can't see if the formatting has worked or not

Code:
 
#!/bin/bash
#Send process which has exceeded 25%
#
#
echo 'The below is a list of processes which have exceeded the 25% threshold' >
/export/home/tjmoore/file2
#
echo ' ' >> /export/home/tjmoore/file2
#
echo 'PRI PID USER NICE %CPU COMMAND' >> /export/home/tjmoore/fi
le2
#
if ps -eo pri,pid,user,nice,pcpu,comm | awk '{if($1>=2) {$1=$1; found=1; print}}
END {exit !found}' OFS="\t" >> /export/home/tjmoore/file2 2>/dev/null
#
then /usr/local/bds/mailsend.s mailx "Process has surpassed 25% threshold" <email address> <email address> /export/home/tjmoore/file2 2&1>/dev/
null
fi
#

---------- Post updated at 04:35 AM ---------- Previous update was at 04:32 AM ----------

Ive altered the percentage CPU the process has to use to be picked up just so I can be sure I will be getting some results, I will alter back when the script works
# 6  
Old 08-02-2012
OK, so we have to analyze stepwise. Any error messages?
The if statement needs a ; before the then part.
To check the logics of the awk stmt,please repeatedly run
Code:
ps ...|awk '{if($5>=2) {$1=$1; found=1; print}} END {exit !found}' OFS="\t"; echo "exit code: "$?

and increase the percentage x ($5>=x) until no line is printed any more. How does the exit code change?
This User Gave Thanks to RudiC For This Post:
02JayJay02 (08-02-2012)
# 7  
Old 08-02-2012
Ive used the code you supplied and get this response, Ive substituted x with numbers 1-10

Code:
awk: syntax error near line 1
awk: illegal statement near line 1
exit code: 2

Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Formatting the Output EmbedUX Shell Programming and Scripting 5 06-05-2013 05:13 AM
Formatting output Daniel Gate Shell Programming and Scripting 4 02-03-2013 01:43 PM
Help Formatting Output KathyB148 AIX 2 01-18-2013 09:11 AM
Formatting output tdev457 Shell Programming and Scripting 3 10-29-2011 03:00 PM
sql select command output formatting in shell script rkrish Shell Programming and Scripting 1 10-12-2011 01:59 AM
Output Formatting mooey1232003 Shell Programming and Scripting 7 05-05-2011 05:15 AM
Output formatting . pinga123 Shell Programming and Scripting 5 03-25-2011 08:09 AM
Formatting of output maverick_here Shell Programming and Scripting 5 03-03-2011 06:27 PM
formatting output aikaterinimak Shell Programming and Scripting 4 11-11-2010 07:02 PM
Formatting my output rahiljavaid Shell Programming and Scripting 5 04-19-2010 05:40 AM
formatting output swkambli Shell Programming and Scripting 1 01-25-2010 06:32 PM
Formatting ls output bonrad Shell Programming and Scripting 4 11-26-2009 07:04 AM
Formatting Output dhanamurthy Shell Programming and Scripting 6 05-02-2008 11:43 AM
formatting output balaji_prk Shell Programming and Scripting 4 09-15-2007 09:23 AM
Formatting output illur81 Shell Programming and Scripting 3 10-13-2005 09:24 AM