Formatting the output of ps -eo command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Formatting the output of ps -eo command
# 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 10: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 11: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:
# 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 Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Output formatting

Hi , can anyone help me in this Input file Name= XYZ Company= ALPHA City= Stockhomn Equipment=computer Country=Sweden Name=MNC Company=Beta City=Kaulampur Equipment=computer Country=Malaysia Name=JVC Company=gamma City=Kiruna (3 Replies)
Discussion started by: swets
3 Replies

2. Shell Programming and Scripting

Formatting the output

Hi, I have a file which contents entries in this form. Only in /data4/temp abc.000001 Only in /data4/temp abc.000003 Only in /data4/temp abc.000012 Only in /data4/temp abc.000120 Only in /data4/temp abc.000133 Only in /data4/temp abc.001444 i want to read line by line and format... (2 Replies)
Discussion started by: arijitsaha
2 Replies

3. Shell Programming and Scripting

sql select command output formatting in shell script

Hi, I need to connect to the database and retrieve two variables from the database and store them in a variable,out of these two variables I need to get lastdigit appended to the variable 1 retrieved and variable 2 with out any modification in short select var,data from usage; o/p=... (1 Reply)
Discussion started by: rkrish
1 Replies

4. Shell Programming and Scripting

Output formatting .

below is a CPU utilization Log for ABC server. However for every 15 minutes it generates 3 CPU values(with interval of 2 sec). Host CPU CPUtotal CPU% time ABC 101.1 2 50.55 14 : 15 ABC 100.5 2 50.25 14 : 15 ABC 100.2 2 50.1 14 : 15 ABC 100.9 2 50.45 14 : 30 ABC 100.5 2 50.25 14 : 30 ABC... (5 Replies)
Discussion started by: pinga123
5 Replies

5. Shell Programming and Scripting

Output formatting

I have input file in this way John 1234 BASIC 26000 John 1234 ALLOWC 01550 John 1234 INCER 01700 John 1234 REL 20000 Debi 2345 BASIC 29000 Debi 2345 ALLOWC 01600 Debi 2345 INCR 01900 Debi 2345 REL ... (8 Replies)
Discussion started by: vakharia Mahesh
8 Replies

6. Shell Programming and Scripting

formatting output

my script is as follows cnt=`ps -ef |grep pmon|grep -v grep|awk 'END {{print NR}}'` cnt2=`ps -ef |grep tns|grep -v grep|awk 'END {{print NR}}'` if then if then rman target/ catalog recdb/recdb@recdb cmdfile report_need_backup.sql > report_need_backup.txt ... (1 Reply)
Discussion started by: swkambli
1 Replies

7. Shell Programming and Scripting

Formatting ls output

I am using find and ls to search for "warez" files on my server. find /home/ -regex ".*\.\(avi\|mp3\|mpeg\|mpg\|iso\)" -print0 | xargs -0 ls -oh This command produces this: -rw-r--r-- 1 1000 3.2M Feb 18 2009 /home/user/public_html/lupus.mp3 I want to only get this 3.2M... (4 Replies)
Discussion started by: bonrad
4 Replies

8. Shell Programming and Scripting

formatting the output

Is it possible to convert the attached file to the format mentioned. Here for a particular job the table name and the corresponding instance name from one test run "X" is compared with the table name and the instance name from the second test run "Y" for output rows,affected rows,applied... (1 Reply)
Discussion started by: ragavhere
1 Replies

9. Shell Programming and Scripting

Formatting output

Hi, I am new to shell scripting, I ahve written a shell script which would extract me data, but my problem is I want to add a column name to my output. Using grep and sed I am getting my data from the input file. Sample data name : eric name : tom name : sean My output using grep and sed... (3 Replies)
Discussion started by: illur81
3 Replies

10. Shell Programming and Scripting

Formatting the output

Hi all, Have the following code(1) producing the results(2 & 3). Would like to know if there is a way to format the two reports created in a similar fashion. IE - The first is formatted nicely as a result of the echo "$xmpbdate $xavgs" >> $xmpbrpt However when I attempt to do the same on... (7 Replies)
Discussion started by: Cameron
7 Replies
Login or Register to Ask a Question