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
Split a paragraph Sekar1 UNIX for Dummies Questions & Answers 3 06-05-2008 01:35 PM
Extract a paragraph capri_drm Linux 2 06-04-2008 04:01 PM
help, using awk to get paragraph kunimi Shell Programming and Scripting 6 06-04-2008 07:56 AM
script for a 3 line paragraph invinzin21 Shell Programming and Scripting 2 12-18-2007 01:11 AM
Bold the paragraph caprikar Shell Programming and Scripting 3 12-22-2003 06:44 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 06-25-2008
fara_aris fara_aris is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 27
script to list out the output in one paragraph

Hi All,
I want to run 5 `ps -ef | grep [process]` cmds in one script and i want the script to give me return code 0 if everything is OK. If it notices one of the processes is not there, it will prompt me the process name and advice me to check it.

I've wrote a script that separates the output but I want to combine it into one output.

echo "\n***------------------------------***"
echo "\nNo. 8"
echo "To check WAS-server1 process.."
varB=`ps -ef | grep server1`
if [ $? -eq 0 ]
then
echo "OK, WAS-server1 process is up."
else
echo "==>NOT OK; WAS-server1 PROCESS IS DOWN, please check!!"
fi
sleep 3

echo "\n***------------------------------***"
echo "\nNo. 9"
echo "To check tws process.."
varC=`ps -ef | grep tws`
if [ $? -eq 0 ]
then
echo "OK, tws processes are up."
else
echo "==>NOT OK; tws PROCESS IS DOWN, please check!!"
fi
sleep 3


And the output is;
No. 8
To check WAS-server1 process..
OK, WAS-server1 process is up.

***------------------------------***

No. 9
To check tws process..
OK, tws processes are up.


I want the output to be like this;
(If all processes are up)
Checking all processes..
Return code : 0
All processes are up, OK.

(If one of the processes in not up)
Checking all processes..
WAS-server1 is UP, OK.
==> TWS is not UP, PLEASE CHECK!!



Please help, thank you.
  #2 (permalink)  
Old 06-25-2008
spirtle spirtle is offline
Registered User
  
 

Join Date: Jun 2008
Location: Scotland
Posts: 150
Your first problem is that

Code:
ps -ef|grep whatever

will always be successful resulting in $? being always 0, because of your grep process itself. You could try grep -C process name if you know the actual command for the process you are looking for.

As for the output, you could collect the names of all the processes in two lists, e.g.
something along the lines of

Code:
ps -C $process > /dev/null
if[ $? -eq 0 ]
then 
  procs_up="$procs_up $process"
else
  procs_down="$procs_down $process"
fi

if [ ${#procs_down} -eq 0 ]
then
  echo All processes are up, OK.
else
  for p in $procs_up
  do
     echo "$p is up. OK"
  done
  for p in $procs_down
  do
     echo "$p is not up. please check"
  done
 fi

You could also usefully put the process checking bit into a function.
  #3 (permalink)  
Old 06-26-2008
fara_aris fara_aris is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 27
spirtle,

I tried with single process first;
root@axdevt01::/tmp/fara/script> cat grep2.sh
#process=df
process=server1
ps -ef | grep $process | grep -v grep > /dev/null
if [ $? -eq 0 ]
then
procs_up="$procs_up $process"
else
procs_down="$procs_down $process"
fi

if [ ${procs_down} -eq 0 ]
then
echo All processes are up, OK.
else
for p in $procs_up
do
echo "$p is up. OK"
done
for p in $procs_down
do
echo "$p is not up. please check"
done
fi


And this is the output;
root@axdevt01::/tmp/fara/script> grep2.sh
grep2.sh[12]: test: argument expected
server1 is up. OK

Then I tried df (not server1, df is not up), and this is the output;
root@axdevt01::/tmp/fara/script> grep2.sh
grep2.sh[12]: df: bad number
df is not up. please check



Any advise?
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 04:03 AM.


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