In a context where a diversity of terminals might be running simultaneously, a new terminal is launched, executing a program (say gnome-terminal -e pathto/myprogram), now, myprogram is to figure out. unambiguously, the PID of the gnome-terminal it is running on. How can this be done?
- 972
2 Answers
If pathto/myprogram is a shell script, you can use the environment variable '$PPID'.
[root@docker ~]# echo $PPID
20746
[root@docker ~]# ps auxw | grep 20746
root 20746 0.0 0.1 145696 5256 ? Ss 10:38 0:00 sshd: root@pts/0
root 20825 0.0 0.0 112648 964 pts/0 R+ 13:09 0:00 grep --color=auto 20746
[root@docker ~]#
- 349
-
-
@StephenKitt when run from the shell script it outputs a value, but I can't be sure that it is what I'm looking for. Listing the windows with
wmctrl -lpfrom the bash script (which shows the PID), its PID is 0. It's also 0 from the shell that run the script in the first place. – nightcod3r Jul 22 '17 at 18:15 -
3@nightcod3r my point is that xrobau’s solution doesn’t really work, because
$PPIDonly contains the immediate parent’s pid. You’d have to work your way up the pid chain to the terminal. – Stephen Kitt Jul 22 '17 at 18:18 -
@StephenKitt correction: the 0 PID happens only with
mlterm, it works fine withxtermandgnome-terminal. – nightcod3r Jul 22 '17 at 18:18
An application running in a terminal or terminal emulator has its input/output going from/to a /dev/ttysomething or /dev/ptsomething device file.
At the other end (which may involve one or several pseudo-terminals or serial lines or ssh/telnet/rsh connections), something, either a physical or virtual or emulated terminal (possibly written in javascript running in a browser window), running on some local or remote computer will read that output to display characters on a screen, and will send what you type as characters to make up that input.
If you look at it this way, getting a "pid" is not always possible or relevant/useful. You may get a pid, but that could be the pid of a web browser running on a different machine.
There is only a limited number of cases where that can be done and make sense like in your gnome-terminal -e cmd.
gnome-terminal is a special case though as it's got a client/server architecture. gnome-terminal -e cmd, contrary to xterm -e cmd is not running a new terminal emulator that creates a pseudo-tty pair with a child process running cmd with its I/O on the slave side, it's a request to a gnome-terminal server to open a new gnome-terminal window to run cmd.
Also note this warning in more recent version:
# Option “-e” is deprecated and might be removed in a later version of gnome-terminal.
# Use “-- ” to terminate the options and put the command line to execute after it.
If I run gnome-terminal -e zsh twice and run pstree -slpa $$ to see the shell's ancestry, I see:
$ pstree -slpa $$
systemd,1 splash
└─systemd,7571 --user
└─gnome-terminal-,27110
└─zsh,27811
└─pstree,27988 -slpa 27811
$ pstree -slpa $$
systemd,1 splash
└─systemd,7571 --user
└─gnome-terminal-,27110
└─zsh,28134
└─pstree,28145 -slpa 28134
It's the same process that started both zshs, and even though it is multi-threaded:
$ ps -Lp 27110
PID LWP TTY TIME CMD
27110 27110 ? 00:00:01 gnome-terminal-
27110 27111 ? 00:00:00 gmain
27110 27113 ? 00:00:00 dconf worker
27110 27114 ? 00:00:00 gdbus
Both windows are handled by the same thread.
Having the 27110 pid (which you'll find with getppid() or $PPID in most shells) is not going to be very useful. If you kill it, you'll end up killing all the windows it's managing.
If you want to terminate the current window, all you need to do is exit within cmd.
- 544,893
xtermsets the environment variableWINDOWID. Doesgnome-terminaldo the same? You can get from this to the pid withwmctrl -lpoutput. – meuh Jul 22 '17 at 18:26mltermsetsWINDOWIDwhich is was I was looking for in particular. Thx! – nightcod3r Jul 22 '17 at 18:57xprop -id "$WINDOWID" -notype _NET_WM_PID– Stéphane Chazelas Jul 22 '17 at 19:27gnome-terminal. – JdeBP Jul 23 '17 at 11:18screen, say? Would there be some other way to accomplish what you're looking to do? – ilkkachu Jul 23 '17 at 14:00WINDOWIDseems to do the job. – nightcod3r Jul 23 '17 at 20:44