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?

| improve this question | | | | |
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 ~]#
| improve this answer | | | | |
  • 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.