How do I find what process is running in a particular GNU screen window?
Problem
I need to find what process is running on a particular window in screen (in a reasonable amount of time).
Scenario
I need to use the Session Name and Window Title to find the process running therein. It needs to not be super slow.
Also potentially noteworthy: I'm using byobu as a wrapper for screen.
What I've tried
• Searching the internet
• Reading the screen man page (not for the faint of heart). (Okay, I didn't read all of it, but I did most of the relevant sections and searched it very thoroughly for anything that might be useful.)
• What I learned:
• The only way to gain the information I might need from screen (by calling screen) is through the use of it's command line flags.
• -Q will allow you to query certain commands, but none of these provided everything that I need. The one that I'm using returns the Window number.
• number - what I'm using to get the window number
• windowlist - allows you to get a custom-formatted string of information, but the session PID is not one of the things you can ask for
• echo, info, lastmsg, select, time, title are the other ones and none of these looked useful
• -ls lists the active sessions. It prepends the PID to the session name, so this is how I'm currently getting the session PID.
• Once I have the PID of the shell running in a specific Window, I can check its environment variables for WINDOW. This variable is set to the window number. This is what I'm using to match the process to the window.
• There is no single command that will allow me to return the session PID and a map of the window titles to window numbers. Also, I could find no way to deterministically find the session id and window title to window number map outside of calling screen.
• Trial and error / digging through environment variables
• Writing a script
My Script
I wrote a script that seems to successfully solve the problem, but it takes a little over 0.75 seconds to run. This is far too long for what I need done, but more importantly, far too long when a server is waiting for its completion to send the response to an HTML request.
Here is the script:
As you can see, the problem commands are the two screen commands. I can get rid of one of them by searching for a screen process launched with the session name, but this feels kind of flaky and I'm not sure it would be deterministic:
Goal
I would like to have a fast, reliable way to determine the process currently running in a specific screen window. I feel like I'm just missing something, so I would be very grateful if one of you spot it!
(I'm still fairly knew to StackExchange, so any feedback on my question is welcome!)