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 4 2 Answers
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 parentI'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 inputdo
read -p "Type new terminal name > " text 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.