Visit Our UNIX and Linux User Community

shell script to format command output

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell script to format command output
# 1  
Old 12-10-2010
shell script to format command output

Hello team,

I am running below command which is giving following output.

bash-3.00$ ps -eo pid,pcpu,args | sort +1n | grep -i java
12 0.0 grep -i java
8804 0.0 /opt/app/ccr/home/ccr/WebSphere/AppServer/java/bin/sparcv9/java -XX:+UnlockDiag
9241 0.0 /opt/app/ccr/home/ccr/WebSphere/AppServer/java/bin/sparcv9/java -XX:+UnlockDiag
18558 0.0 /opt/app/ccr/home/ccr/WebSphere/AppServer/java/bin/java -Xms128m -Xmx512m -DPOL
26719 0.0 /usr/jdk1.6.0_18/bin/java -Djava.compiler=NONE
64 0.1 /opt/app/ccr/home/ccr/WebSphere/AppServer/java/bin/sparcv9/java -server -Dwas.s

I have requirement to developed script which will check values in column two and if the value exceeds the threshold value , it should send alert with following message echo "$column1 has crossed the threshold value of cpu"

Please help me to prepare script.
# 2  
Old 12-10-2010
ps -eo pid,pcpu,args | sort +1n | grep -i java | awk -v threshold=<VALUE> '$2 > threshold {print $1,"has crossed the threshold value of cpu";}'

# 3  
Old 12-10-2010
Hello Anurag,

Thanks for the response. I replaced the value variable with value 6 and i am getting following error.

I need one more update in script if cpu is below threhold value the output should get written in the file.

bash-3.00$ ps -eo pid,pcpu,args | sort +1n | grep -i java | awk -v threshold=6 '$2 > threshold {print $1,"has crossed the threshold value of cpu";}'
awk: syntax error near line 1
awk: bailing out near line 1
# 4  
Old 12-10-2010
Use nawk instead of awk.
ps -eo pid,pcpu,args | sort +1n | grep -i java | nawk -v threshold=<VALUE> '$2 > threshold {print $1,"has crossed the threshold value of cpu";} $2 <= threshold {print;}'

# 5  
Old 12-10-2010
Thanks Anurag,

This is perfectly working fine. I need one more help,Is it possible to email alert if it cpu has crossed the threshold value.and also when cpu is under threshold it should write the same in file. It would be great if you can explain me the execution of command.
# 6  
Old 12-10-2010
This will put all alerts and command outout in same file
ps -eo pid,pcpu,args | sort +1n | grep -i java | nawk -v threshold=<VALUE> '$2 > threshold {print $1,"has crossed the threshold value of cpu";} $2 <= threshold {print;}' > outputFile

If you need it in different files then
All alerts in one file
ps -eo pid,pcpu,args | sort +1n | grep -i java | nawk -v threshold=<VALUE> '$2 > threshold {print $1,"has crossed the threshold value of cpu";}' > alertFile

Rest in another file
ps -eo pid,pcpu,args | sort +1n | grep -i java | nawk -v threshold=<VALUE> '$2 <= threshold {print;}' > outputFile

And content of alertFile can be sent as email using mailx command.
Something like (verify mailx path):
/usr/sbin/mailx -s "Your Subject" ADDRESS_EMAIL < alertFile

# 7  
Old 12-11-2010
Thanks for the response anurag.script is really working fine.

Previous Thread | Next Thread
Test Your Knowledge in Computers #106
Difficulty: Easy
A kernel is at the heart of the Linux OS; and this kernel acts as an interface between the user and underlying hardware.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to convert any shell command output to JSON format?

Hi All, I am new to shell scripting, Need your help in creating a shell script which converts any unix command output to JSON format output. example: sample df -h command ouput : Filesystem size used avail capacity Mounted /dev/dsk/c1t0d0s0 8.1G 4.0G 4.0G 50% /... (13 Replies)
Discussion started by: balu1234
13 Replies

2. Programming

Python or Shell script to Grep strings from input file and output in csv format

Hi Experts, I am writing a python script to grep string from file and display output in csv file as in attached screenshot Input file(result_EPFT_config_device) Below is the python script i have prepared as of... (1 Reply)
Discussion started by: as7951
1 Replies

3. Shell Programming and Scripting

How to pipe command output to shell script?

Hi Team, Need a help on how to pipe a command out put to a shell script. My shell script looks like below. cat shell_script #!/usr/bin/ksh input =$@ echo " we are inside the shell script" echo " here are the input parameters" .......................... .................. ... (11 Replies)
Discussion started by: gvkumar25
11 Replies

4. Shell Programming and Scripting

UNIX shell script to format output report

i am new to unix shell scripting, could someone please help me. i was asked to develop a unix script and requirement of the script is as follows: 1. In source directory, if any new files are being dropped by external system then an email should be sent out with a message saying "files are... (3 Replies)
Discussion started by: crefi1545
3 Replies

5. Shell Programming and Scripting

Shell script to replace output of command

Hi I would like to write a shell script which replace particular text in output of a shell command. For example : If I execute pwd (if dir is /users/master/) it should display as - /users/Master_directory/. Also, for generalising the script I would like to receive parameters when I run the... (3 Replies)
Discussion started by: ratneshnagori
3 Replies

6. Shell Programming and Scripting

Need help with shell script - output of top command

I have written shell script to send file as an attachemt of email and output of "top -o res" command as email body. it works fine if i execute manually from prompt but it does not send "top -o res" command output in email body when it is executed via crontab. Any suggestions. My script is below:... (5 Replies)
Discussion started by: needyourhelp10
5 Replies

7. Shell Programming and Scripting

Process diff command output in a shell script

diff -yta file1 file2 #!/usr/abc/b/bin/perl5.6 | #!/usr/abc/b/bin/perl5.8 Notable thing about above line is "|" appears at 62nd position. When the same line is assigned in a variable in a ksh script, using ss=$(diff -yta file1 file2) it appears as ... (4 Replies)
Discussion started by: bhaliyajalpesh
4 Replies

8. Shell Programming and Scripting

Evaluating command output (shell script )

Hello, in a script i would like to evaluate a command output with a grep, for example, to know if the parameter defined by the user is in the output. Something like: the_command | grep $1 Please, how is the way to evalulate in this a script, like if echo "incorrect parameter, not in... (2 Replies)
Discussion started by: aristegui
2 Replies

9. Shell Programming and Scripting

reading command output from shell script

Hi List, How to read the output of a command executed from a script. For ex. ------------- useradd testuser1 password testuser1 .... ..... -------------- This prompts for "password", and "confirm password", for which i need to give the values from script. Can... (4 Replies)
Discussion started by: sri b
4 Replies

10. Shell Programming and Scripting

Capturing shell script command output

I am trying to check to see if a file exists on a ftp server, well, I know that cant be done, atleast directly, So I came up with this small script ftp -n $HOST <<END_SCRIPT quote USER $USER quote PASS $PASSWD cd public_html/crap dir $FILE quit END_SCRIPT Where the $ variable... (2 Replies)
Discussion started by: designflaw
2 Replies

Featured Tech Videos