Tell me more ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Given an X11 window ID, is there a way to find the ID of the process that created it?

Of course this isn't always possible, for example if the window came over a TCP connection. For that case I'd like the IP and port associated with the remote end.

The question was asked before on Stack Overflow, and a proposed method was to use the _NET_WM_PID property. But that's set by the application. Is there a way to do it if the application doesn't play nice?

share|improve this question
1  
I've marked ephemient's answer as accepted because it clarified an important confusion of mine. But it doesn't answer the “if … doesn't play nice” part, so I'll change this if someone comes up with an answer to that part. – Gilles Jan 8 '11 at 12:32
Related: PID from window ID/name – Gilles Aug 29 '12 at 23:16

4 Answers

up vote 5 down vote accepted

The _NET_WM_PID isn't set by the window manager (as just another X11 client, how would it know?).

Instead, compliant X11 clients (applications) are expected to set _NET_WM_PID and WM_CLIENT_MACHINE on their own windows. Assuming a well-behaved application, this will be true whether a window manager is running or not.

If WM_CLIENT_MACHINE is your own hostname, then the PID should be meaningful.
Otherwise, "I'd like the IP and port associated with the remote end" — I'm not sure what that means. For example, if you have an ssh session open with X forwarding enabled, windows opened by forwarded apps will be marked with remote PID and hostname, but you don't necessarily have any way to connect back to that remote host.

share|improve this answer
1  
_NET_WM_PID is set by the application: right, that does make more sense! But it's not the X11 protocol, it's the relatively recent FreeDesktop specification. – Gilles Jan 8 '11 at 12:26
In the ssh case, as far as the X server is concerned, this is a local connection from the sshd process. Though _NET_WM_PID seems to be set to the remote PID and WM_CLIENT_MACHINE to the remote connection (tested with xterm). – Gilles Jan 8 '11 at 12:26

If you have xdotool installed, then

xdotool selectwindow getwindowpid

followed by clicking on the window in question will return the PID.

(There are other ways of selecting the window in question, e.g., if you have its window ID you can just do xdotool getwindowpid <number>. You can also select by name or class, etc.)

I do think this requires some playing nice on behalf of the WM. I haven't experimented much, or needed to.

share|improve this answer
xdo_getwinprop(xdo, window, atom_NET_WM_PID, &nitems, &type, &size) ⇒ it's just a shell wrapper to read _NET_WM_PID (useful, but not what I asked for). – Gilles Jan 6 '11 at 23:44

xdotool didn't work for me. This did:

Run

xprop _NET_WM_PID

and click on the window.

This is based on the answer at http://www.linuxquestions.org/questions/linux-software-2/advanced-question-finding-pid-of-an-x-window-328983/

share|improve this answer
Excellent, thanks! – Andrew Schulman May 15 at 8:16

I was able to use the xdotool under Ubuntu 11.04 beta, but selectwindow was not a valid command, I had to hack a script with:

$ while true; do sleep 1; xdotool getactivewindow; done

then watch the window ID go by while I selected the window I wanted, then decoded the responsible PID with:

$ xdotool getwindowpid <the-window-id>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.