[SOLVED] bash how to wait in script from the previous command to finish
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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.
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.
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 .
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.
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 .
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 .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.