1

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?

nightcod3r
  • 952
  • 2
  • 16
  • 32
  • 1
    `xterm` sets the environment variable `WINDOWID`. Does `gnome-terminal` do the same? You can get from this to the pid with `wmctrl -lp` output. – meuh Jul 22 '17 at 18:26
  • @meuh `mlterm` sets `WINDOWID` which is was I was looking for in particular. Thx! – nightcod3r Jul 22 '17 at 18:57
  • Related: [How can we know who's at the other end of a pseudo-terminal device?](/q/136662) – Stéphane Chazelas Jul 22 '17 at 19:24
  • 2
    @meuh, or `xprop -id "$WINDOWID" -notype _NET_WM_PID` – Stéphane Chazelas Jul 22 '17 at 19:27
  • Aside from the fact that [terminals do not have process IDs](https://superuser.com/a/723442/38062), only _terminal emulators_ have; there is also the fact that since [its adoption of a client-server architecture](https://unix.stackexchange.com/questions/201900/) the terminal emulator program in GNOME Terminal is _not_ `gnome-terminal`. – JdeBP Jul 23 '17 at 11:18
  • What do you need the terms PID for? What if you're running inside `screen`, say? Would there be some other way to accomplish what you're looking to do? – ilkkachu Jul 23 '17 at 14:00
  • @ilkkachu I'm looking for the window PID of the terminal emulator, once it has been launched, and from the program that is running on it. It acts as a console and need control of the window (position and other features). By now, `WINDOWID` seems to do the job. – nightcod3r Jul 23 '17 at 20:44
  • related: [How can we find out the pseudoterminal master and slave from each other?](https://unix.stackexchange.com/questions/492302/) –  Jun 12 '19 at 13:00

2 Answers2

0

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 ~]#
xrobau
  • 349
  • 2
  • 7
  • Try it from a shell script rather than the shell prompt... – Stephen Kitt Jul 22 '17 at 17:56
  • @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 -lp` from 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 `$PPID` only 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 with `xterm` and `gnome-terminal`. – nightcod3r Jul 22 '17 at 18:18
0

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.

Stéphane Chazelas
  • 522,931
  • 91
  • 1,010
  • 1,501