Wednesday 24 April 2019

bash - gnu screen: find which screen window has a given running process or variable?


I would like to find out which of my ~50 gnu screen windows has a process running with a specific variable defined in it. For example, about a week ago, I did this:


id=ABC123456; ~/run_long_process $id

This is running in one of my ~50 gnu screen windows and it's producing a lot of STDOUT/STDERR but, other than scrolling back each of the windows or Ctrl+Z and resuming each of the windows, is there a way of finding out which one it is? Any suggestions?



Answer



If this is Linux, you could follow a process something like this. As an example of a "long running process" I'm going to use "perl -e sleep" which just sleeps forever:


$ id=ABC123456; perl -e sleep $id

Now, we need to find the running process:


$ ps -Af | grep [A]BC123456
user 30579 22013 0 09:32 pts/10 00:00:00 perl -e sleep ABC123456
# ^^^^^ parent PID

Now that we have the parent's PID, we can snoop in its environment, in which screen sets a WINDOW variable:


$ tr '\0' '\n' < /proc/22013/environ | grep WINDOW
WINDOW=3

Which is correct. I ran it in screen Window 3. Since this is an environment variable, there's a good chance that your task will also inherit it (depending on which flavor of exec() calls are used), so you can probably snoop the environment of your task as well, and find the same result.


No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...