LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-16-2009, 11:56 AM   #1
JDska55
LQ Newbie
 
Registered: Jun 2009
Location: Iowa City, IA
Distribution: SuSE 11, ubuntu Hardy
Posts: 28

Rep: Reputation: 15
making bash wait until a string changes


Hey all-
I'm trying to figure out a way to get bash to wait until a string returned from a command changes from 'pending' to 'running' before it executes any other lines. So far, I've tried a couple while loops and a for loop, but they don't seem to be working. The string output looks like this:
Code:
$ ec2din --simple
i-218jsb4     pending
i-2123ns1     terminated
i-12mj43a     running ec2-124.123.12.09@compute-1.amazonaws.com
Obviously, I'm paying attention to the second field only here. My last attempt looked like this:

Code:
for i in $(ec2din --simple | awk '$2 ~ /pending/ {print $2}');
do
sleep 10
done
This doesn't seem to work, it just exits at what appears to be almost an arbitrary time, long before the string changes from 'pending' to 'running' Any thoughts on why this is misbehaving?

Cheers,
Jarrod
 
Old 06-16-2009, 12:26 PM   #2
jamescondron
Member
 
Registered: Jul 2007
Location: Scunthorpe, UK
Distribution: Ubuntu 8.10; Gentoo; Debian Lenny
Posts: 961

Rep: Reputation: 69
You'll probably find the arbitrary time is 10seconds

How about a while loop, as in, while a value == "some string"?
 
Old 06-16-2009, 01:40 PM   #3
JDska55
LQ Newbie
 
Registered: Jun 2009
Location: Iowa City, IA
Distribution: SuSE 11, ubuntu Hardy
Posts: 28

Original Poster
Rep: Reputation: 15
You're probably right on the 10s front, though I'm not going to get out my stopwatch and check it lol.

I did try a couple while loops, looking something like this:

Code:
while [$(ec2din --simple | awk '$2 /pending/')]
do
sleep 10
done
I figured that once the pending was gone, that condition would evaluate to false, kicking it out of the while loop. However, that didn't seem to be the case, it gave me a couple of syntax errors and didn't execute properly. I'm not exactly sure how to set up the condition in the while loop using awk or another parsing tool. Any thoughts?
Jarrod

Last edited by JDska55; 06-16-2009 at 01:57 PM.
 
Old 06-16-2009, 02:11 PM   #4
jamescondron
Member
 
Registered: Jul 2007
Location: Scunthorpe, UK
Distribution: Ubuntu 8.10; Gentoo; Debian Lenny
Posts: 961

Rep: Reputation: 69
What errors did it give you?
 
Old 06-16-2009, 03:21 PM   #5
JDska55
LQ Newbie
 
Registered: Jun 2009
Location: Iowa City, IA
Distribution: SuSE 11, ubuntu Hardy
Posts: 28

Original Poster
Rep: Reputation: 15
[i-9d7729f4: command not found. So apparently that particular while loop doesn't even parse the line correctly. Hm.
 
Old 06-16-2009, 03:44 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
What do you expect "$2 /pending/" to do? It's not a comparison ... in
fact I'd expect awk to error out on the /pending/.



And what does the whole line with pending in it look like?



Cheers,
Tink
 
Old 06-16-2009, 09:01 PM   #7
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 333

Rep: Reputation: 141Reputation: 141
Try
Code:
grep -q 'running' <(ec2din --simple)
-q Do not write anything to standard output. Exit immediately with zero status if any match is found, even if an error was detected.
ec2din should continue in its subshell after grep has found 'running' and exited.

The above runs silently.
If you want the output from ec2din to go to the terminal try this.
(It will give a 'tee: standard output: Broken pipe' error which can be ignored.)
Code:
terminal=$(tty)
grep -q 'running' <(ec2din --simple | tee $terminal)

Last edited by Kenhelm; 06-16-2009 at 10:17 PM. Reason: Changed '< <' to '<'
 
Old 06-17-2009, 11:13 AM   #8
JDska55
LQ Newbie
 
Registered: Jun 2009
Location: Iowa City, IA
Distribution: SuSE 11, ubuntu Hardy
Posts: 28

Original Poster
Rep: Reputation: 15
@Tink-
I guess I assumed that it would return *something* until the pending was no longer found, at which point it would return 0 and kick out of the loop. I'm not very familiar with awk's inner workings, I must admit. The line that would contain the "pending" looks like this:

i-218jsb4 pending

@Kenhelm
That would work great if I only have one running instance, but won't that immediately kick out if there's already a running instance as well as a pending? I'll usually have 2-5 instances running already when I am booting another one up, which is why i'm trying to key off of 'pending' rather than 'running'

Cheers,
Jarrod
 
Old 06-17-2009, 11:40 AM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Can you explain what you're trying to achieve with this
loop, what's the objective of "do something while jobs
are pending"?


Code:
while $( ec2din --simple | awk 'BEGIN{flag=0}{if($0 ~ /pending/){flag++}}END{if ( flag > 0 ) {exit 0} else {exit 1}}' );
do
sleep 10
done
Untested, might work
 
Old 06-17-2009, 02:34 PM   #10
JDska55
LQ Newbie
 
Registered: Jun 2009
Location: Iowa City, IA
Distribution: SuSE 11, ubuntu Hardy
Posts: 28

Original Poster
Rep: Reputation: 15
Tink-
What i'm trying to do here is make the script hang until the instances booting up in Amazon's cloud are fully running. While they are booting up, ec2din --simple returns the following:
Code:
i-12n1asj     terminated
i-12ncajk     pending
i-15na52s     running ec2-aRandomAddress.compute-1.amazonaws.com
Once they are running, ec2din --simple returns
Code:
i-12n1asj     terminated
i-12ncajk     running ec2-anotherRandomAddress.compute-1.amazonaws.com
i-15na52s     running ec2-aRandomAddress.compute-1.amazonaws.com
I need the script to wait because the next lines are scp and ssh commands that use the DNS address in the 3rd column (as well as a running instance... trying to ssh into a machine that isn't running would be, well, kind of pointless )

But HEY!
that loop actually worked, even with other running instances. I really have no idea what that condition means, could you explain it a bit? But even a black box that works is 10x better than what I had before. Thanks Tink!
 
Old 06-17-2009, 02:48 PM   #11
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Well, the construct is a tad more involved because we need to check whether ANY
of the lines have a pending in them.

Awk (under normal mode of operation, with no other qualifiers) operates based on
lines of input, so we need to make sure we check ALL lines of input before we make
a decision.

A bit more about awk; awk per-line actions take place in a pair of curly braces {}.
If you need to define things before any processing, or take action after all lines
have been processed (this is very simplified) you can use a BEGIN{} and an END{}
block respectively.

What I've done above it to define a "flag" variable that will be set if any lines
of input have pending in them.

Assume all processes are running - direct exit
Code:
'BEGIN{flag=0}
Check whether any processes match "pending", if so,
increment flag.
Code:
{if($0 ~ /pending/){flag++}}
If flag is set (any pending processes) tell the while loop about it.
Code:
END{if ( flag > 0 ) {exit 0} else {exit 1}}'


If you have more specific questions - ask. After all, that's the
name of the site ;}



Cheers,
Tink
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create a bash script to wait for wget to finish and then continue danlee Linux - General 14 05-30-2008 11:21 AM
How to get bash to wait for something to happen? drakebasher Linux - Newbie 4 02-18-2006 08:00 AM
bash-script won't wait for application to finish TLV Linux - Software 24 09-30-2004 11:18 PM
while I wait for bash programming jay2901 Programming 4 08-07-2004 07:23 AM
wait in bash shells raypen Linux - Software 1 04-18-2003 02:19 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration