Bash Script to start application after two checks

Thread Solved

Join Date: Mar 2009
Posts: 3
Reputation: rman10 is an unknown quantity at this point 
Solved Threads: 0
rman10 rman10 is offline Offline
Newbie Poster

Bash Script to start application after two checks

 
0
  #1
Mar 17th, 2009
Hello!

I am a newbie to scripting and I'm trying to start an application via a bash script after checking to make sure that two condititions do (or do not) exist first. I've attempted to write down what I'd like to do and was hoping someone could point me in the right direction. I don't know how to take the output from my awk commands and use them within the bash script to determine whether or not to start the process. Here's what I'm trying to do:
Script name = dynamips.sh
Start dynamips using port 7200 as the port variable

./dynamips.sh 7200
# if proc utilization over 50% error break out and tell user to try another server
# I'm thinking of doing this with awk and somehow acting on the "exceed" that is printed

ps -ef | awk '/Cpu/ {if ($2 > 50 ) print "exceed"}'

# if number of dynamips processes are 5 or more break out and tell user to try another server
## I'm thinking of doing this with awk and somehow acting on the "exceed" that is printed

ps -ef | awk '/dynamips/ {print $10}' | awk 'END {print NR,"Total"}' | awk '/Total/ {if ( $1 > 4) print "exceed"}'

else

# execute /usr/bin/dynamips -H $port

# and then a way to stop/start/restart

Any help is greatly appreciated!

TIA,
Richard
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: Bash Script to start application after two checks

 
0
  #2
Mar 18th, 2009
Hey Richard,

For both of those, you can capture the output of your test commands using backticks (will work in any shell) or the $() test in bash

For instance, you could take your statement:

ps -ef | awk '/Cpu/ {if ($2 > 50 ) print "exceed"}'
and assign it to a variable

xceeded=`ps -ef | awk '/Cpu/ {if ($2 > 50 ) print "exceed"}'`
or

xceeded=$(ps -ef | awk '/Cpu/ {if ($2 > 50 ) print "exceed"}')
and then test that variable, for example:

Shell Scripting Syntax (Toggle Plain Text)
  1. if [ $xceeded == "exceed" ]
  2. then
  3. do what have to do
  4. fi

Let me know if that's enough of a nudge in the right direction. You're almost there

Best wishes,

Mike
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 3
Reputation: rman10 is an unknown quantity at this point 
Solved Threads: 0
rman10 rman10 is offline Offline
Newbie Poster

Re: Bash Script to start application after two checks

 
0
  #3
Mar 18th, 2009
Originally Posted by eggi View Post
Hey Richard,

For both of those, you can capture the output of your test commands using backticks (will work in any shell) or the $() test in bash

For instance, you could take your statement:



and assign it to a variable



or



and then test that variable, for example:

Shell Scripting Syntax (Toggle Plain Text)
  1. if [ $xceeded == "exceed" ]
  2. then
  3. do what have to do
  4. fi

Let me know if that's enough of a nudge in the right direction. You're almost there

Best wishes,

Mike

Thanks Mike! I'm almost there... I've changed it just a bit to the following:

#!/bin/bash
#dynamips.sh
xceedPROC=$(ps -ef | awk '/dynamips/ {print $10}' | awk 'END {print NR,"Total"}' | awk '/Total/ {if ( $1 > 4) print "exceed"}')
xceedCPU=$(mpstat | awk '/all/ {if ($4 > 20) print "exceed"}')
if [ ($xceedPROC == "exceed") -o ($xceedCPU == "exceed") ]
then
echo "Server Resources out of tolerance, please try another Server"
else
`nice /$HOME/dynamips/dynamips -H $1 &`
fi

My only problem now is the 'if' line is not working. I'm trying to have it check if $xceedPROC or $xceedCPU = exceed then stop.

I'm going to keep researching and will let you know if I figure it out.

Thanks,
Richard
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 3
Reputation: rman10 is an unknown quantity at this point 
Solved Threads: 0
rman10 rman10 is offline Offline
Newbie Poster

Re: Bash Script to start application after two checks

 
0
  #4
Mar 18th, 2009
OK. I got it to work. My previous 'if' line was working correctly. My problem was that I was getting an 'exceed' variable when I thought I shouldn't have been. I neglected to account for the bash shell nambe "dynamips.sh" which was showing up in my awk for 'dynamips'.

This is my very first script of any kind and it's been frustrating but a good learning experience!

Thanks for your help Mike!

Here's my final version:

#!/bin/bash
#dynamips.sh (start with sudo ./dynamips.sh "TCP port #" &
xceedPROC="" # used to ensure variable is empty (probably not necessary)
xceedCPU="" # same as above
# following line checks for running process and if there are more than 3 it places "exceed" in the variable "xceedPROC"
xceedPROC=$(ps -ef | awk '/bin\/dynamips/ {print $10}' | awk 'END {print NR,"Total"}' | awk '/Total/ {if ( $1 > 3 ) print "exceed"}')
# following line checks cpu util and if it's more than 20% it places "exceed" in the variable "xceedCPU"
xceedCPU=$(mpstat | awk '/all/ {if ($4 > 20) print "exceed"}')
if [ "$xceedPROC" = "exceed" ] || [ "$xceedCPU" = "exceed" ]
then
echo "Server has exceeded Min Resources, Please try another Server"
else
`nice $HOME/dynamips/dynamips -H $1`
fi
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: Bash Script to start application after two checks

 
0
  #5
Mar 19th, 2009
Cool,

Good job I was going to suggest this for your if statement, but I guess I should have read farther down

if [ $xceedPROC="exceed" -o $xceedCPU="exceed" ]
Either way's good

Best wishes,

Mike
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Shell Scripting Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC