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 06-25-2017, 03:38 PM   #1
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Rep: Reputation: Disabled
[Solved] Not waiting for Xterm to finish , bash script


Hi doing some bash script and i am using Xterm to execute some scripts .
One of my problems is that when i launch a xterm window from that bash script , the bash script does not continue unless xterm window is finished what is supposed to do .
How to make the bash script continue the instructions and not wait for xterm ?

Quote:
#!/bin/bash
xterm -T "SC1" -geometry 100x10 -e "./script1.sh;bash"
echo "Script 1 was launched"
xterm -T "SC2" -geometry 100x10 -e "./script2.sh;bash"
echo "Script 2 was launched"
echo "all scripts are running"
The problem on the previous code is that the command "echo "Script 1 was launched" will only displayed after xterm finishes script1.sh .

I don't want the bash script to wait for xterm to finish.

Last edited by pedropt; 06-26-2017 at 04:16 AM.
 
Old 06-25-2017, 03:50 PM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
I don't know about the scripts that the executed terminals are running but this should work because adding & sends it into its own shell separating it from the parent. ( someone else might be able to put that into words better, but that is the jist of it)

Code:
 #!/bin/bash
xterm -T "SC1" -geometry 100x10 -e "./script1.sh;bash" &
echo "Script 1 was launched"
xterm -T "SC2" -geometry 100x10 -e "./script2.sh;bash" &
echo "Script 2 was launched"
echo "all scripts are running"
launch term output
Code:
userx%slackwhere ⚡ testing ⚡> ./launch-eterm
Script 1 was launched
Script 2 was launched
all scripts are running
userx%slackwhere ⚡ testing ⚡>
if it is not working then you/we/someone needs to then look at the other scripts to see what they are doing.

Last edited by BW-userx; 06-25-2017 at 04:01 PM.
 
Old 06-25-2017, 04:27 PM   #3
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
The main bash script have a lot to process , that was an example script .
There should be another way to do it without having to execute 2 commands at same time .
Quote:
xterm -T "SC1" -geometry 100x10 -e "./script1.sh;bash" &
echo "Script 1 was launched"
I can not use this in my main script because it have a lot to run after those xterms are launched .
 
Old 06-25-2017, 04:30 PM   #4
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
where is the real deal so you can get real results?
 
Old 06-25-2017, 05:02 PM   #5
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
Quote:
where is the real deal so you can get real results?
you are kidding right ?!
i have 300 lines of script , you surely are not thinking that i will post it here .
 
Old 06-25-2017, 05:54 PM   #6
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
well you lost me there, you have say 2 scripts for some reason you seem to want to use another script to execute a terminal to execute a different script then execute another terminal to execute another script.


that is how it works it is called procedural programing, it runs the first command first when return code gets returned then it runs the next command next.

you'll just have to run them off two separate terminals separately if you want them running simultaneously.

in my testing just now though I did try this

remove the shebang in your 'main script' you are running to get the other two running and see what that does for you, as I created two other scripts to run a for loop to 10 echoing 10 times, it seemed to start faster as a loop count to 10 is really really really fast on my laptop so it is hard to see, after i removed the shebang in the script that fires the other two scripts off.

it did seem to start up more together then the first runs to end before starting the second one.

Code:
uxterm -T "SC1" -geometry 100x10 -e "./echoscript1;bash" &
echo "Script 1 was launched"
uxterm -T "SC2" -geometry 100x10 -e "./echoscript2;bash" &
echo "Script 2 was launched"
echo "all scripts are running"
notice no shebang

Last edited by BW-userx; 06-25-2017 at 06:01 PM.
 
Old 06-25-2017, 11:42 PM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Code:
xterm -T "SC1" -geometry 100x10 -e "./script1.sh;bash"
Step1: remove extra 'bash':
Code:
xterm -T "SC1" -geometry 100x10 -e "./script1.sh"
Step2: add &
Code:
xterm -T "SC1" -geometry 100x10 -e "./script1.sh" &
Step3: drop 'xterm'
Code:
./script1.sh &
Edit:

> "I can not use this [ampersand] in my main script because it have a lot to run after those xterms are launched."

You should explain what have you misunderstood here.

Last edited by NevemTeve; 06-25-2017 at 11:45 PM.
 
Old 06-26-2017, 01:36 AM   #8
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
Eventually if i wanted to use gnome-terminal instead xterm then i do not have this problem .
With gnome terminal i can launch both scripts and the main script will continue its programmed code .
 
Old 06-26-2017, 02:03 AM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Please explain why do you think you should use any terminal-emulator to execute a program asynchronously?
 
Old 06-26-2017, 04:16 AM   #10
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
Quote:
Please explain why do you think you should use any terminal-emulator to execute a program asynchronously?
Because the main bash script is not waiting for any result from the Xterm windows .
Bash script is doing one thing and xterm windows are doing another completely different .

As always , i had to figure out a way to do it .

The solution was to create an independent bash script from the original one with the code to launch only the xterm windows , and that independent bash script will open with gnome terminal .
This way the main script can continue itself .

Basically is something like this :

main script
Quote:
#!/bin/bash
gnome-terminal -t "xt scripts" --working-directory=$path -x bash -c "./xt.sh; bash"
echo "Scripts launched"
....continue the code....
2nd script to launch xterm : xt.sh
Quote:
#!/bin/bash
xterm -T "SC1" -geometry 100x10 -e "./script1.sh;bash" &
xterm -T "SC2" -geometry 100x10 -e "./script2.sh;bash" &
xterm -T "SC3" -geometry 100x10 -e "./script3.sh;bash" &
xterm -T "SC4" -geometry 100x10 -e "./script4.sh;bash"
 
Old 06-26-2017, 04:43 AM   #11
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
> > Please explain why do you think you should use any terminal-emulator to execute a program asynchronously?

> Because the main bash script is not waiting for any result from the Xterm windows .
> Bash script is doing one thing and xterm windows are doing another completely different .

This absolutely doesn't answer my question. you can start any number of programs in the background without using any terminal emulators:
Code:
for i in 1 2 3; do 
   { echo "$$ start"; sleep 3; echo "$$ end"; } & 
done
echo "main program does something else"
wait
 
Old 06-26-2017, 07:17 AM   #12
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,779

Rep: Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304Reputation: 7304
that is called background process and you use & to start it:
Code:
background_process1 &
script2 &
app3 &
process4 &
main_code is coming here
wait for any background processes (if you wish)
why is this not acceptable?
 
  


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
[SOLVED] bash how to wait in script from the previous command to finish pedropt Programming 24 03-23-2017 04:19 PM
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
Howto avoid waiting for a process to finish in a script anil3 Linux - General 3 12-07-2007 05:35 AM
bash script suggestions for waiting for ssh -L iggymac Programming 4 03-22-2005 07:04 PM
bash-script won't wait for application to finish TLV Linux - Software 24 09-30-2004 11:18 PM

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

All times are GMT -5. The time now is 03:34 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