0

I am trying to use and bash file .sh to start other bash files in seperate terminals. I want them to run one after the other as I use them to copy large numbers of files to the same Hard Drive. However the following Bash starts all at the same time.

#!/bin/bash    
gnome-terminal --tab --title="1" -- bash /media/dave/Cloud/Music/2.MakeAlbums/NewMaster1.sh &  
wait  
gnome-terminal --tab --title="2" -- bash /media/dave/Cloud/Music/2.MakeAlbums/NewMaster2.sh &  
wait  
echo "Complete"

However my Ubuntu Server (latest release) is not respecting the wait command.

9
  • Is there actually anything to wait for? Does the &'d command actually continue running in the first place, if you look at the process list? Commented Apr 22 at 20:00
  • 3
    Having a command & immediately followed by a wait is pointless, it's the same as simply command. Your problem is that the gnome-terminal command returns immediately, rather than when the command it launches (e.g. NewMaster1.sh) terminates in its window, but the wait command can't help you here, it only knows about the direct child gnome-terminal which returns immediately. Luckily gnome-terminal has a --wait option to make it wait for its child, this is what you need.
    – egmont
    Commented Apr 22 at 20:42
  • 1
    While at it, taking a step back... I generally consider it a bad idea for a shell script to open terminal windows. It should be the other way around. The script should copy the files, one after the other. It's the user's choice to pick his/her favorite terminal emulator, whatever that terminal is; open that terminal and run the script in that; eventually having the script's output visible in a single scrollback area. Alternatively he/she could even run the script without a terminal.
    – egmont
    Commented Apr 22 at 21:07
  • 2
    @egmont "Having a command & immediately followed by a wait is pointless, it's the same as simply command" – Unless there is a trap. Here there is none, so you're right; but in general something & wait may make sense. Commented Apr 22 at 21:12
  • 1
    @KamilMaciorowski Wow, it's always great to learn something new, thanks a lot! (Another use case that occurred to me is if you have multiple commands running in the background, then wait waits for any of them to terminate, it's again obviously different from running them sequentially in the foreground.)
    – egmont
    Commented Apr 23 at 4:57

1 Answer 1

0

This still runs in the background using the & symbol.
The wait %1 uses the most recent bg process. What's the -- used for?

#!/bin/bash

(gnome-terminal --tab --title="1" -- bash /media/dave/Cloud/Music/2.MakeAlbums/NewMaster1.sh &); wait %1
echo "Job 1 complete."

(gnome-terminal --tab --title="2" -- bash /media/dave/Cloud/Music/2.MakeAlbums/NewMaster2.sh &); wait %1
echo "Job 2 complete."

echo "Finished."

This would be, essentially, the same thing with no background process.

gnome-terminal --tab --title="1" -- bash /media/dave/Cloud/Music/2.MakeAlbums/NewMaster1.sh && gnome-terminal --tab --title="2" -- bash /media/dave/Cloud/Music/2.MakeAlbums/NewMaster2.sh; echo "Complete"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .