I was wondering if it is possible to sort of restart gnome-terminal from itself? What I mean by that is, is there a command which I can use to tell gnome-terminal to close and then immediately relaunch itself? I have tried running:

exit && gnome-terminal

But that doesn't work because after it has exited it can't launch itself from itself, so I was wondering if there is another command which will close it but also instruct another program to then immediately open it again?

OS Information:

No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 15.10
Release:    15.10
Codename:   wily
Flavour: GNOME
GNOME Version: 3.18

Package Information:

  Installed: 3.18.2-1ubuntu2~wily1
  Candidate: 3.18.2-1ubuntu2~wily1
  Version table:
 *** 3.18.2-1ubuntu2~wily1 0
        500 http://ppa.launchpad.net/gnome3-team/gnome3-staging/ubuntu/ wily/main amd64 Packages
        100 /var/lib/dpkg/status
     3.16.2-1ubuntu4 0
        500 http://archive.ubuntu.com/ubuntu/ wily/main amd64 Packages
  • 2
    What about gnome-terminal && exit? – Byte Commander Dec 2 '15 at 19:36
  • @ByteCommander: It needs to be closed before the new session is started. – user364819 Dec 2 '15 at 19:37
  • What about bash -c "sleep 2 && gnome-terminal" & exit then? – Byte Commander Dec 2 '15 at 19:40
  • That also doesn't restart gnome-terminal-server... Although it opens the second window after the first closes. – Byte Commander Dec 2 '15 at 19:51
  • You could & disowning it. Some terminal programs can behave a bit weirdly when starting each other directly though, see here – Wilf Dec 2 '15 at 20:45

You need to "detach" newly spawned terminal window. Now what command does that ? nohup or setsid

nohup gnome-terminal && exit

setsid gnome-terminal && exit

  • Why nohup / setsid? Just gnome-terminal && exit ;) – kos Dec 3 '15 at 20:06

trap the pseudo-signal EXIT:

trap 'gnome-terminal' EXIT

Now everytime you run exit a new instance of gnome-terminal will be opened by shell and the existing session of shell will be terminated.

As @Byte Commander has pointed out, if you do exit (as you were doing) or press Ctrl + D to exit the shell, the above would create a gnome-terminal window with the same PID as the earlier one (although the shell would get a different PID), this behavior is by design. So to kill the current gnome-terminal process and start a new one, you can do:

trap 'kill $(ps -o ppid= $$) && gnome-terminal' EXIT
  • Nice, but this does only restart gnome-terminal-server (I'm checking it's PID in an xterm before and afterwards to determine this) if you close the gnome-terminal window. It does not do this if you run exit or type CTRL+D in the window. – Byte Commander Dec 2 '15 at 19:50
  • Your edit seems to say the opposite of my comment? If the first window gets closed because the shell terminates (e.g. by running exit), your trap opens a new gnome-terminal window before the first one closes and therefore the underlying gnome-terminal-server instance stays the same and does not restart. It only restarts if the first window was closed by terminating the gnome-terminal instance, e.g. by clicking the (X) window decoration. – Byte Commander Dec 2 '15 at 19:56
  • @ByteCommander That edit was not directed towards you..anyway good catch..check my edits.. – heemayl Dec 2 '15 at 21:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy