There are many ways to obtain the PTS associated with a given process - such as using the tty
command or resolving /proc/self/fd/0
(assuming it's open and not redirected) - but what about the inverse? How do I obtain the PID of the shell running the pts?
I can try to use something like ps -t $(tty)
but this does not provide enough information to reliably determine the root shell (i.e. multiple shell instances, PID reuse on long running systems, etc). The only way I can think of to this is to manually collect all processes associated with the terminal and visit each process's /proc
entry to determine process hierarchy and then assume it's the topmost parent process.
Is there a simple inverse tty
command I can use? One that gives me the PID without too much work?
ps -t $(tty) --forest
help with that?ps
guarantees that the topmost process returned is also the root of the forest, but I'm unsure if there aren't any edge cases I should account for. If there's a proper way to query linux's tty subsystem, or have a link to it, it would be much more preferable.