1

I need to open a new terminal from my script but I want to wait for that terminal to exit before continuing with the rest of the script. I know that

gnome-terminal -e 'nano test.txt'

opens a new terminal window with "test.txt" opened in nano, but the calling script does not get blocked. Is there a way to wait for the new terminal to exit, before continuing the original script?

ravindu1024
  • 1,496
  • 1
  • 13
  • 30
  • When I try your above command then command execution is blocked until "nano" and "gnome-terminal" are finished. Why do you think it is not so? – pitseeker Jul 23 '15 at 09:39
  • 3
    This is not possible. It is a bug or a feature of GNOME, depending on whom you ask. https://bugzilla.redhat.com/show_bug.cgi?id=903157 – n. m. could be an AI Jul 23 '15 at 09:44
  • @pitseeker - no it doesn't. It does not block on the Ubuntu terminal. – ravindu1024 Jul 23 '15 at 10:00
  • Blocking support of `gnome-terminal` got removed on purpose by the Gnome project. Only the first instance blocks, all further (parallel) instances hand everything over to this single open instance and then immediately terminate. – Tino Jun 17 '17 at 10:44

3 Answers3

2

For the benefit of the reader: The answer from @Feiteira used to be the correct answer, but is no more supported by gnome-terminal. Option --disable-factory got removed from gnome-terminal.

To explain the different observation of several people here:

gnome-terminal only blocks, if it is the first instance of it. This single instance then acts as a server for all future invocations of gnome-terminal. Those other gnome-terminal calls then hand everything over to this server and immediately terminate. So you can observe both behavior: "blocking" (when it is the one-and-only single instance) and "nonblocking" (when another instance is already running).

There is a wrapper, for example used by Ubuntu 16.04, which emulates this missing option. However this wrapper is complex. This is done by starting another server (with another service name). And for this server, the same assumption holds (you can attach other gnome-terminals to this server, and those others then come back immediately as well).

My suggestion is to stop using gnome-terminal and to switch to something, which works right out of the box. For example xfce4-terminal officially supports all those little absolutely necessary options for a mature terminal window:

xfce4-terminal --disable-server -x nano test.txt

Also xfce4-terminal introduces --hold which allows you to see the output of a command. Sadly --hold has a bug (at least under Ubuntu 16.04), which causes it to (often) truncate the output (the terminal is closed too early if a command terminates, so all still buffered output of the PTY does not make it to the window. This is a very well known common bug if you are not careful enough with PTYs).

Tino
  • 9,583
  • 5
  • 55
  • 60
1

You could use && for instance (also see What is the purpose of "&&" in a shell command?):

gnome-terminal -e 'nano test.txt' && sleep 5 && echo "Done"
Community
  • 1
  • 1
philonous
  • 3,621
  • 3
  • 27
  • 24
  • This only blocks the call for the duration of sleep. The 'nano test.txt' part of the function does not block. – ravindu1024 Jul 23 '15 at 09:58
  • As you said you are using this in a script I put the commands in a bash script. And yes, it did block. Otherwise I would not have posted this answer. But as n.m. pointed out it is a feature/bug of GNOME – and since I do not use GNOME at all (except for gnome-terminal to verify my answer) it worked on my side but it obviously does not on yours. – philonous Jul 23 '15 at 10:28
  • I am using Linux Mint 17.2 and it has gnome-terminal version 3.6.2 and I can assure you that it only blocks for the 5s sleep. After that the original script continues behind the window which runs nano. Move the nano window and you will see. – ravindu1024 Jul 23 '15 at 10:55
  • I completely trust you on that. But as has been pointed out by n.m. this is a GNOME issue (and most probably this is the cause for the problem – Linux Mint is also GNOME based afaik). Long story short: Couldn't you use `xterm` for instance, or any other terminal? – philonous Jul 23 '15 at 11:02
  • of course I could. But I was hoping to use gnome-terminal to do this since thats the default terminal emulator in Ubuntu based distros. I was just replying to your earlier comment where you said it works on your machine. – ravindu1024 Jul 23 '15 at 11:10
  • Yes it works on my machine, but I am not using GNOME. I only installed gnome-terminal but nothing more. When using GNOME with gnome-terminal this behavior is not feasible and you will have to use another terminal. – philonous Jul 23 '15 at 11:22
  • Yes, that was what nm said as well. I agree. But in your earlier comment you said "(except for gnome-terminal to verify my answer) it worked on my side but it obviously does not on yours.". If I'm not mistaken, what you are saying is that it worked on your machine on gnome-terminal. Isn't it? – ravindu1024 Jul 23 '15 at 13:28
  • Yes it works on my machine, but I have KDE installed (and only gnome-terminal from GNOME, nothing more). – philonous Jul 23 '15 at 13:39
1

The option you want is: --disable-factory

gnome-terminal --disable-factory ...
Feiteira
  • 811
  • 8
  • 9
  • FYI: Gnome officially removed support for this. Ubuntu 16.04 added a workaround for this. However this is unofficial, as it is not part of the manual pages anymore. – Tino Jun 17 '17 at 10:50
  • 1
    I had quite forgotton about this question until recently. I can confirm that --disable-factory works on my Gnome Terminal on Linux Mint 18.1 which is based on Ubuntu 16.04. – ravindu1024 Jun 19 '17 at 00:57
  • It seems recent versions of `gnome-terminal` have grown a `--wait` option.. – TTimo Aug 04 '23 at 14:10