1

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:

gnome-terminal:
  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 Answers2

6

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
heemayl
  • 91,753
  • 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 02 '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 02 '15 at 19:56
  • @ByteCommander That edit was not directed towards you..anyway good catch..check my edits.. – heemayl Dec 02 '15 at 21:38
4

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

nohup gnome-terminal && exit

setsid gnome-terminal && exit

Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497