943,917 Members | Top Members by Rank

Ad:
Mar 17th, 2009
0

Bash Script to start application after two checks

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rman10 is offline Offline
3 posts
since Mar 2009
Mar 18th, 2009
0

Re: Bash Script to start application after two checks

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:

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

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

Quote ...
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
Reputation Points: 102
Solved Threads: 47
Posting Whiz
eggi is offline Offline
399 posts
since Oct 2007
Mar 18th, 2009
0

Re: Bash Script to start application after two checks

Click to Expand / Collapse  Quote originally posted by eggi ...
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rman10 is offline Offline
3 posts
since Mar 2009
Mar 18th, 2009
0

Re: Bash Script to start application after two checks

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rman10 is offline Offline
3 posts
since Mar 2009
Mar 19th, 2009
0

Re: Bash Script to start application after two checks

Cool,

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

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

Best wishes,

Mike
Reputation Points: 102
Solved Threads: 47
Posting Whiz
eggi is offline Offline
399 posts
since Oct 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Shell Scripting Forum Timeline: awk to decimal places
Next Thread in Shell Scripting Forum Timeline: How to Create a shell script to test the telnet connection status





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC