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

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:

if [ $xceeded == "exceed" ]
then
    do what have to do
fi

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

Best wishes,

Mike

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:

if [ $xceeded == "exceed" ]
then
    do what have to do
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

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

Edited 3 Years Ago by mike_2000_17: Fixed formatting

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

This question has already been answered. Start a new discussion instead.