4

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?

8

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.

1
  • Note that grep [A]BC123456 is being used with [A] instead of A so that the regular expression being searched for doesn't match the grep process itself. Also note that the e flag to ps (e.g. ps -Af e), on Linux at least, will cause it to show each process's environment, although there will be lots of variables, so you might need to use e.g. grep --color WINDOW so it's easier to find the relevant variable. – doshea Feb 2 '17 at 3:40

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.