Finding the terminal your script is running in - Programming On Unix
Users browsing this thread: 2 Guest(s)
|
|||
This is question that came up in a german board: https://forum.ubuntuusers.de/topic/pruef...mein-srip/
I suspect the original author will be satisfied with a pragmatic solution. I'm forwarding the question to this forum, though, because I hope we can come up with something nice and clever. :) Just for fun. The goal is to find the PID of the terminal your script is running in. Turns out, this is not as easy as it sounds. The naïve approach is to traverse the process tree. The parent process of your script is ... yeah, what is it? Most likely an interactive shell. But it could also be the terminal you're looking for if you started your script using something like "xterm -e foo.sh". On the other hand, if you launched the script from inside of Vim, there's an additional intermediate process. Then there's SSH. Then there's screen and tmux. Then there's screen over SSH. Then there's the Linux VT where the terminal emulator is not an actual process. And so on. You get the idea. That's what I got so far: Code: #!/bin/bash The idea is to traverse the process tree and look for clues. Terminal emulators usually hold an open file descriptor to /dev/ptmx, so that's what I try to find. I used a Bash script, but any common language is fine. Ideally, the solution runs on *BSD as well, but I'd be happy to settle for just Linux. |
|||
Messages In This Thread |
Finding the terminal your script is running in - by movq - 02-07-2016, 02:23 PM
RE: Finding the terminal your script is running in - by venam - 03-07-2016, 01:56 AM
RE: Finding the terminal your script is running in - by movq - 03-07-2016, 12:41 PM
RE: Finding the terminal your script is running in - by venam - 03-07-2016, 01:12 PM
RE: Finding the terminal your script is running in - by venam - 12-08-2020, 08:11 AM
RE: Finding the terminal your script is running in - by z3bra - 12-08-2020, 08:43 AM
RE: Finding the terminal your script is running in - by movq - 13-08-2020, 06:45 AM
RE: Finding the terminal your script is running in - by venam - 05-06-2021, 05:26 AM
|