Welcome!
Important information
-- Spectre and Meltdown vulnerabilities
-- Change in MX sources

News
-- MX Linux on social media: here
-- Mepis support still here

Current releases
-- MX-17.1 Final release info here
-- antiX-17 release info here

New users
-- Please read this first, and don't forget to add system and hardware information to posts!
-- Here are the Forum Rules

wait for terminal in a script

Feel free to talk about anything and everything in this board. Just don't post offensive topics that are meant to cause trouble with other members or are derogatory towards people of different genders, race, color, minors (this includes nudity and sex), politics or religion. Let's try to keep peace among the community and for visitors.

No spam on this or any other forums please! If you post advertisements on these forums, your account may be deleted.

Do not copy and paste entire or even up to half of someone else's words or articles into posts. Post only a few sentences or a paragraph and make sure to include a link back to original words or article. Otherwise it's copyright infringement.

You can talk about other distros here, but no MX bashing. You can email the developers of MX if you just want to say you dislike or hate MX.
Message
Author
Vincent17
Forum Novice
Forum  Novice
Posts: 48
Joined: Tue Feb 20, 2018 1:07 am

wait for terminal in a script

#1 Post by Vincent17 » Mon Oct 01, 2018 6:01 pm

I have a script which should open an interactive terminal, then wait for it to close before continuing. Using

Code: Select all

xfce4-terminal -x bash -ic "command1"
command2 
command2 executes immediately. The only reasonable solution I have found is to insert

Code: Select all

while [[ -n $(pgrep command1) ]] ; do sleep 1 ; done
before command2. But if command1 is running elsewhere, I have to resort to

Code: Select all

xfce4-terminal -x bash -ic "command1" UniqueID
while [[ -n $(ps a | awk '/UniqueID/&&!/awk/') ]] ; do sleep 1 ; done
command2 
which is ugly.

How is a neater way to accomplish this?

sunrat
Forum Regular
Forum Regular
Posts: 228
Joined: Mon Mar 28, 2016 9:54 pm

Re: wait for terminal in a script

#2 Post by sunrat » Mon Oct 01, 2018 6:55 pm

So you want command1 to complete before running command2?

Code: Select all

command1 && command2
Somehow I feel that's not what you want as it seems too obvious.
If your "ugly" code works as intended, be happy. It's not as if you want to take it to the royal ball or anything. :D

User avatar
fehlix
Forum Guide
Forum Guide
Posts: 2348
Joined: Wed Apr 11, 2018 5:09 pm

Re: wait for terminal in a script

#3 Post by fehlix » Mon Oct 01, 2018 7:10 pm

Vincent17 wrote:
Mon Oct 01, 2018 6:01 pm
How is a neater way to accomplish this?
something like

Code: Select all

xfce4-terminal -e 'bash -c "echo Count_down; for i in {9..0}; do echo $i; sleep 1; done; read -p Ready_for_Take_Off_Hit_any_key " '
echo Beam_me_Up_Scotty
:anispider2:
EDit: Opps, doesn do waht you want as it don't wait ...
Gigabyte Z77M-D3H, Intel Xeon E3-1240 V2 (Quad core), 32GB RAM,
GeForce GTX 770, Samsung SSD 850 EVO 500GB, Seagate Barracuda 4TB

User avatar
Adrian
Forum Veteran
Forum Veteran
Posts: 9170
Joined: Wed Jul 12, 2006 1:42 am

Re: wait for terminal in a script

#4 Post by Adrian » Mon Oct 01, 2018 7:13 pm

Why not:

Code: Select all

xfce4-terminal -x bash -ic "command1"; command2 
And if you want to run command2 only if command1 returns true use && instead of ;

User avatar
fehlix
Forum Guide
Forum Guide
Posts: 2348
Joined: Wed Apr 11, 2018 5:09 pm

Re: wait for terminal in a script

#5 Post by fehlix » Mon Oct 01, 2018 7:43 pm

OK, I need to wait until scotty is ready ...

Code: Select all

xfce4-terminal -x bash -ic 'echo Count_down; for i in {9..0}; do echo $i; sleep 1; done; read -p Ready_for_Take_Off_Hit_any_key ; touch /tmp/Ready_for_Take_off ' 
while [ ! -f /tmp/Ready_for_Take_off ] ; do :  ; sleep .1 ; done 
echo Beam_me_Up_Scotty
This will wait unti Ready_for_Take_off before Scotty "Beams me Up"
:anispider2:
Gigabyte Z77M-D3H, Intel Xeon E3-1240 V2 (Quad core), 32GB RAM,
GeForce GTX 770, Samsung SSD 850 EVO 500GB, Seagate Barracuda 4TB

User avatar
fehlix
Forum Guide
Forum Guide
Posts: 2348
Joined: Wed Apr 11, 2018 5:09 pm

Re: wait for terminal in a script

#6 Post by fehlix » Mon Oct 01, 2018 9:26 pm

Now we got a signal before Take_Off:

Code: Select all

xfce4-terminal -x bash -ic 'echo Count_down; for i in {9..0}; do echo $i; sleep 1; done; read -p Ready_for_Take_Off_Hit_any_key ; [ -f /tmp/Waiting_for_TakeOff ] && { xargs kill -s CONT < /tmp/Waiting_for_TakeOff ;   rm -f /tmp/Waiting_for_TakeOff ;   } '

echo $$ >> /tmp/Waiting_for_TakeOff 
kill -s STOP $$
echo Beam_me_Up_Scotty
:anispider2:
Gigabyte Z77M-D3H, Intel Xeon E3-1240 V2 (Quad core), 32GB RAM,
GeForce GTX 770, Samsung SSD 850 EVO 500GB, Seagate Barracuda 4TB

Vincent17
Forum Novice
Forum  Novice
Posts: 48
Joined: Tue Feb 20, 2018 1:07 am

Re: wait for terminal in a script

#7 Post by Vincent17 » Mon Oct 01, 2018 10:14 pm

Thanks for the repies!
@Adrian Does that work for you? Here, "terminal terminated?" appears immediately

Code: Select all

xfce4-terminal -x bash -ic "sleep 10" ; echo "terminal terminated?"
@ fehlix Your script certainly does what I want, and I love the Star Trek theme, but I'm not sure it's light years neater ;) Seriously, an indicator file is a good idea.
sunrat wrote:If your "ugly" code works as intended, be happy.
I get confused when a script exceeds about 40 lines, so every line counts!

User avatar
Adrian
Forum Veteran
Forum Veteran
Posts: 9170
Joined: Wed Jul 12, 2006 1:42 am

Re: wait for terminal in a script

#8 Post by Adrian » Mon Oct 01, 2018 11:03 pm

Vincent17 wrote:
Mon Oct 01, 2018 10:14 pm
Thanks for the repies!
@Adrian Does that work for you? Here, "terminal terminated?" appears immediately

Code: Select all

xfce4-terminal -x bash -ic "sleep 10" ; echo "terminal terminated?"
No, you are right, the command is forked, returns 0 for success and then the next command is executed immediately.

User avatar
Adrian
Forum Veteran
Forum Veteran
Posts: 9170
Joined: Wed Jul 12, 2006 1:42 am

Re: wait for terminal in a script

#9 Post by Adrian » Mon Oct 01, 2018 11:11 pm

This works though...

Code: Select all

xfce4-terminal --disable-server -x bash -ic "sleep 10" ; echo "terminal terminated?"

User avatar
penguin
Forum Regular
Forum Regular
Posts: 286
Joined: Wed Jan 04, 2017 3:15 pm

Re: wait for terminal in a script

#10 Post by penguin » Tue Oct 02, 2018 1:35 am

I use the follow:

read -t 3 -p "Wait for 3 seconds or Hit Enter ...."

The script will wait 3 seconds for input or will execute next line.

Post Reply

Return to “General”