How to close terminal from a bash script

10,272

Solution 1

I'll assume that you're running those commands in a script.

Keep in mind that $$ is the pid of the running bash process. If you're running a script, that script's bash process is a child of your current interactive shell. If you kill $$ in the script, you're killing the script, not the parent shell.

Bash stores the parent pid in the $PPID variable, so you want

#!/bin/bash
gnome-terminal &   # launch a new terminal
kill $PPID         # kill this script's parent

I'm assuming that the parent shell is the one spawned from the terminal, and that you haven't changed the terminal's default behaviour of closing when its shell exits.

As an aside, instead of

echo -n "Type new terminal name > "  # displays messagebox
read text                            # load messagebox input

do

read -p "Type new terminal name > " text

Solution 2

I did find a suitable solution for this problem thanks to the given answers. It simply could be done with the command kill -9 $var (where var is $PPID).

I did edit the code from my start-post to the script i'm using now. Thx for all your input.

Share:
10,272

Related videos on Youtube

Underscore
Author by

Underscore

I like electronics, computers, VB.NET, ASM, and since i'm new to linux i'm experimenting with scripts.

Updated on September 18, 2022

Comments

  • Underscore
    Underscore almost 2 years

    I'm new to this site and to Linux.

    I'm trying to make a simple script what will open a new terminal with a new name, and close the old terminal from where the script was running.

    The problem I encounter is that the process number is changing. So if I start the process and type: echo $$ I see 10602. After the end of the process if the new terminal is loaded the process number is changed to 10594. So I'm actually killing the wrong process..

    At this moment I use this code:

    echo -n "Type new terminal name > "  # displays messagebox
    read text                            # load messagebox input
    echo "$text" > /etc/terminalname     # write messagebox input to file
    
    gnome-terminal                       # open terminal with new name
    
    kill -9 $PPID                        # this will kill the old terminal
    exit                                 # exit script
    
    • Rinzwind
      Rinzwind over 8 years
      why so difficult? I would use "nohup" + "&" to start the 2nd one and just use an "exit" in the 1st script.
    • Eduardo Cola
      Eduardo Cola over 8 years
      You could use killall instead of kill, but that will kill all instances of the given program. Something like killall gnome-terminal.
    • Underscore
      Underscore over 8 years
      @Rinzwind, i did try the nohup command. It is working but as far i found the terminal with the old name does not close or disapear.
    • Underscore
      Underscore over 8 years
      @Eduardo Cola, it seems like a kill command can't work when it is applied in it's own terminal, or a script running from that terminal.
  • kos
    kos over 8 years
    There's no need to background gnome-terminal, it just starts a new instance without taking over the terminal / blocking the script.
  • Underscore
    Underscore over 8 years
    @glenn, It's true that i'm running a script. i found that a terminal can't be closed by a kill command in it's own terminal. The code i posted is written in a .sh file. After executing the code a new terminal is spawned. From the new terminal i can kill the old terminal with "kill" If i type the same "kill" In the old terminal it won't close. It seems like it is a option to execute a new script from the new terminal what kills the old terminal. Or maybe it's a option to perform a key press event like "Ctrl + Shift + Q" in the script so the old terminal will close?