Bash script to start application

March 17, 2009 at 08:11:28
Specs: Ubuntu 8.10
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


See More: Bash script to start application

Report •


#1
March 17, 2009 at 23:40:46
How about posting an example of the ps -ef output, and I'll try to help you. I'd like to see where Cpu is in the string.

Report •

#2
March 18, 2009 at 06:09:18
Sorry... The first command is 'top' and not 'ps -ef'. I'm away from a *nix computer at the moment so I can send a copy of top. (I don't think you need it anyway).

Thanks!


Report •

#3
March 18, 2009 at 13:02:11
Ok, it's working. This is my very first script and it's been challenging but a good learning experience too!

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


Report •

Related Solutions

#4
March 23, 2009 at 11:07:07
Ok, it's working. This is my very first script and it's been challenging but a good learning experience too!

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


Report •


Ask Question