| | |
Bash Script to start application after two checks
Thread Solved |
•
•
Join Date: Mar 2009
Posts: 3
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Oct 2007
Posts: 399
Reputation:
Solved Threads: 47
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:
Let me know if that's enough of a nudge in the right direction. You're almost there
Best wishes,
Mike
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"}'
•
•
•
•
xceeded=`ps -ef | awk '/Cpu/ {if ($2 > 50 ) print "exceed"}'`
•
•
•
•
xceeded=$(ps -ef | awk '/Cpu/ {if ($2 > 50 ) print "exceed"}')
Shell Scripting Syntax (Toggle Plain Text)
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
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!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
•
•
Join Date: Mar 2009
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
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)
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
•
•
Join Date: Mar 2009
Posts: 3
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Oct 2007
Posts: 399
Reputation:
Solved Threads: 47
Cool,
Good job
I was going to suggest this for your if statement, but I guess I should have read farther down 
Either way's good 
Best wishes,
Mike
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" ]

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!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
![]() |
Other Threads in the Shell Scripting Forum
- Previous Thread: awk to decimal places
- Next Thread: How to Create a shell script to test the telnet connection status
| Thread Tools | Search this Thread |





