LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-17-2017, 07:17 PM   #1
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 292

Rep: Reputation: Disabled
bash how to wait in script from the previous command to finish


Hi everyone

Ayone here have an idea how to hang a bash script until previous command is finished ?

I an opening a gnome-terminal from a bash script and i need the current script to wait until gnome-terminal finishes its job

example :

Quote:
gnome-terminal -e "msfconsole -r test.rc > out.tmp"
cat out.tmp
what happens here is that cat command is not waiting for gnome terminal to finish its job , and then says that out.tmp does not exist .

Is there a way to make cat command wait until gnome terminal finishes ?
 
Old 03-17-2017, 09:14 PM   #2
nodir
Member
 
Registered: May 2016
Posts: 222

Rep: Reputation: Disabled
probably the && operator will do that, give it a try.
man bash
Code:
       AND  and  OR lists are sequences of one or more pipelines separated by the && and || control operators, respectively.  AND and OR lists
       are executed with left associativity.  An AND list has the form

              command1 && command2

       command2 is executed if, and only if, command1 returns an exit status of zero.
there is also "wait", but i got no experience with it.
user$ type wait
wait is a shell builtin
 
Old 03-17-2017, 09:39 PM   #3
frankbell
LQ Guru
 
Registered: Jan 2006
Location: Virginia, USA
Distribution: Slackware, Ubuntu MATE, Mageia, and whatever VMs I happen to be playing with
Posts: 17,232
Blog Entries: 27

Rep: Reputation: 5332Reputation: 5332Reputation: 5332Reputation: 5332Reputation: 5332Reputation: 5332Reputation: 5332Reputation: 5332Reputation: 5332Reputation: 5332Reputation: 5332
"&&" definitely.

From the bash man page.

Quote:
command1 && command2
command2 is executed if, and only if, command1 returns an exit status of zero.

Last edited by frankbell; 03-17-2017 at 09:40 PM.
 
Old 03-17-2017, 10:01 PM   #4
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 292

Original Poster
Rep: Reputation: Disabled
Thanks for the reply , but i already try that without success .

Quote:
gnome-terminal -e "msfconsole -r test.rc > out.tmp" && cat out.tmp
 
Old 03-17-2017, 10:19 PM   #5
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 5,514
Blog Entries: 3

Rep: Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784Reputation: 2784
Do you want the text to be displayed inside the actual gnome-terminal that you have launched? If so, move everything to inside the quotes:

Code:
gnome-terminal -e "msfconsole -r test.rc > out.tmp && cat out.tmp"
 
Old 03-17-2017, 10:21 PM   #6
agillator
Member
 
Registered: Aug 2016
Distribution: Mint 19.1
Posts: 419

Rep: Reputation: Disabled
I think you actually have two commands on the line. The first is 'gnome-terminal -e "msfconsole -r test.rc > out.tmp"' and the second is 'cat out.tmp'. If the first runs successfully then the second will run. Gnome-terminal runs successfully regardless of the outcome of msfconsle . . . . Therefore the cat instruction always tries to run.

I would suggest you break the actions down. I'm guessing you are running this in a terminal to begin with. If so, run the msfconsole command first. Then set a variable to the result ($?). Then use an if statement to decide whether to run the cat command based on the value of of the result variable. It is a slightly more complicated approach, but it should do what I think you want. Of course these commands could be put in a script and then run it by the gnome-terminal command.
 
Old 03-17-2017, 10:22 PM   #7
agillator
Member
 
Registered: Aug 2016
Distribution: Mint 19.1
Posts: 419

Rep: Reputation: Disabled
Turbocapitalist answered while I was typing mine. His answer is simpler although both really do the same thing.
 
Old 03-18-2017, 03:00 AM   #8
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 16,897
Blog Entries: 10

Rep: Reputation: 5026Reputation: 5026Reputation: 5026Reputation: 5026Reputation: 5026Reputation: 5026Reputation: 5026Reputation: 5026Reputation: 5026Reputation: 5026Reputation: 5026
different approach:
gnome-terminal is probably already running as a daemon in the background, that's why it doesn't work.
Use a different terminal, like xterm.
 
Old 03-18-2017, 04:26 AM   #9
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,241

Rep: Reputation: 5453Reputation: 5453Reputation: 5453Reputation: 5453Reputation: 5453Reputation: 5453Reputation: 5453Reputation: 5453Reputation: 5453Reputation: 5453Reputation: 5453
I don't know what is msfconsole, so just ask: why do you need terminal at all?
Code:
msfconsole -r test.rc > out.tmp && cat out.tmp 
# should work too, or probably:
msfconsole -r test.rc | tee out.tmp
# is enough
 
Old 03-18-2017, 07:25 AM   #10
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 292

Original Poster
Rep: Reputation: Disabled
If i use xterm i can do it , but somehow with xterm i am having some issues in msfconsole and this is the reason why i want to test with gnome terminal .

Quote:
msfconsole -r test.rc > out.tmp && cat out.tmp
does not work because this command will launch gnome-terminal , and after release gnome-terminal it will execute the next command no matter if gnome-terminal is still running or not .

I need some code that open gnome terminal , waits for it to close and then run the second command .
Only this way is possible .

One idea was getting pid of gnome terminal and wait for it do close and then run the 2nd command .

i need the main script to wait until those commands are finished , and the problem is that the main script is not waiting .
 
Old 03-18-2017, 07:55 AM   #11
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Code:
gnome-terminal -e "msfconsole -r test.rc > out.tmp" ; sleep 30s ; cat out.tmp
adjust sleep to suit.

May have the closing quote in the wrong place...
Might not be an "accident" either.

Question is
How to process Success or Failure of
msfconsole -r test.rc > out.tmp

Can get weird.
but I'd use /tmp/out.tmp and test it on the way "in"

[[ -f /tmp/out.tmp ]]
write it
do_work
rm it on the way out.

Code:
gnome-terminal -e "cat $(msfconsole -r test.rc > out.tmp)"
may work

Just sayin'

Last edited by Habitual; 03-18-2017 at 08:02 AM.
 
Old 03-18-2017, 08:06 AM   #12
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,590

Rep: Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908Reputation: 1908
Quote:
Originally Posted by pedropt View Post
i need the main script to wait until those commands are finished , and the problem is that the main script is not waiting .
No, the main script is waiting, the problem is that the "gnome-terminal" command is some kind of funny wrapper which doesn't wait for the gnome terminal to close before exiting.

http://stackoverflow.com/a/8415989/319698
Quote:
there is no way to get gnome-terminal to behave differently.
 
Old 03-18-2017, 10:23 AM   #13
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 292

Original Poster
Rep: Reputation: Disabled
Quote:
gnome-terminal -e "msfconsole -r test.rc > out.tmp" ; sleep 30s ; cat out.tmp
or

Quote:
gnome-terminal -e "msfconsole -r test.rc > out.tmp"
sleep 30s
cat out.tmp
will not work if gnome-terminal takes more than 30 seconds to execute the command .


And alternative idea is :

Launch gnome terminal with the command .

then the next lines in script would be wait on a loop for out.tmp file to be created .
when out.tmp gets created then loop will stop and script will continue .

Anyone have an idea how to make something like this ?

The loop could be created i a function with if cause :

main script
Quote:
function outf(){
out=out.tmp
if [ -f $out ]; then
echo "Continuing the script"
else
echo "waiting for output from msfconsole"
sleep 2
outf
fi
}
bla
bla
bla
gnome-terminal -e "msfconsole -r test.rc > out.tmp"
outf
cat out.tmp
The only problem here would be if by some reason the msfconsole get an error and do not create the file .
In that case the outf function will be waiting forever .

Last edited by pedropt; 03-18-2017 at 10:34 AM.
 
Old 03-18-2017, 10:44 AM   #14
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 292

Original Poster
Rep: Reputation: Disabled
i think i got it

Quote:
function outf(){
out=out.tmp
sleep=$((sleep+1))
if [ $sleep == "10" ] then
echo "timeout - was not possible to get msfconsole output"
exit 1
fi

if [ -f $out ]; then
echo "Continuing the script"
else
echo "waiting for output from msfconsole"
sleep 2
outf
fi
}
bla
bla
bla
gnome-terminal -e "msfconsole -r test.rc > out.tmp"
sleep="1"
outf
cat out.tmp
There could be some errors in sentences because i made the code now just by hand and logic .

The logic is :
To wait for the result , i created a function to check for the file
but before that function is launched , i created a variable with a number .
Every time the function loops because file does not exist , it increases that variable one number ahead .
This means , that starts with 1 , and in every loop it add +1 = 2
when it gets to 10 then exits the script .
It means that when "sleep" gets to 10 it will be 20 seconds because i let a sleep 2s on the "file not found" if cause .
In mean time if file is created before that time then it continues the main script .

Note: the most hard thing in bash code is creating a code with logic .

Last edited by pedropt; 03-18-2017 at 10:54 AM.
 
Old 03-18-2017, 11:55 AM   #15
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 16,241

Rep: Reputation: 5453Reputation: 5453Reputation: 5453Reputation: 5453Reputation: 5453Reputation: 5453Reputation: 5453Reputation: 5453Reputation: 5453Reputation: 5453Reputation: 5453
so why do you need terminal at all?
 
  


Reply

Tags
bash, command, gnome terminal, wait


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
Wait for “git clone” to finish in bash luftwaffe Linux - Software 3 05-25-2016 11:18 AM
Wait for a remote script to finish before executing another vikas027 Programming 4 08-07-2012 01:48 AM
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
bash-script won't wait for application to finish TLV Linux - Software 24 09-30-2004 11:18 PM
Pppd does not wait for chat script to finish ruchika Programming 1 08-28-2003 11:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:11 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
Open Source Consulting | Domain Registration