I want to know which desktop a given process is running in. I can get this from the window ID using wmctrl. I can also find out which terminal emulator process a given process is running in by tracing the parental lineage up to a terminal process.

However, a given terminal process will have multiple windows spread across multiple desktops.

I use the terminal emulator roxterm these days, although both Konsole and gnome-terminal are similar to it in this respect.

Is there a way to query one or more of these apps to find out which of their windows a given process is running in? If it requires gdb hackery, well, hack away. Though something likely to continue to work after an update would probably be better :)

I'm not entirely opposed to tracking this stuff at bash initialization, but I'm loathe to sort out all the gory details, and it seems likely that there are some significant edge cases that won't work with such an approach. Though maybe somebody has already figured this out?

Alternatively, if there is another terminal emulator that provides a richer interface for accessing such introspective data, that would be good to know.

I could get around this by running each window in a separate process, but the compromises both in terms of memory usage and versatility (eg moving a tab from one window to another) are probably unacceptable. Mostly it's the memory usage.


Many terminal emulators (including rxvt, xterm, gnome-terminal and konsole; I haven't tested with roxterm) set the environment variable WINDOWID to the id of the window the subprocess (e.g. the shell) was started in. So on Linux,

</proc/$pid/environ tr \\0 \\n | sed -n 's/^WINDOWID=//p'

tells you which window $pid is running in.

This won't work if the process has migrated between windows, for instance if it's running under screen or if you migrated tabs between windows.

  • Indeed. It also means that I have to store it somewhere as part of my .bashrc, and deal with any weird inconsistencies. That's helpful to know though, it's more reliable than using wmctrl :ACTIVE:. edit: ..err wait, I just skimmed the answer. Umm actually that's really helpful, thanks. Maybe I could run inotify on those files...?? Is that a crazy idea? – intuited Aug 8 '10 at 22:23
  • The effects of having screen in play are interesting: this means that I'm actually looking for a list of window IDs. I was gonna let that go, for now :) – intuited Aug 8 '10 at 22:24
  • err.. nevermind, inotify wouldn't work unless something in the process environment changes when something like that happens, which I doubt. I'd have to check the parent (screen, roxterm, etc.) environment for changes, assuming it tracks that there, which seems unlikely. – intuited Aug 8 '10 at 22:30

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.