Is there a linux command to determine the window IDs associated with a given process ID?

前端 未结 6 1995
借酒劲吻你
借酒劲吻你 2020-12-22 23:41

Given a process iD of XX, I\'d like to have a list of any window id\'s where _NET_WM_PID = XX. Even better would be the oldest still active window id if possible.

I

6条回答
  •  悲哀的现实
    2020-12-23 00:18

    Here is a list of all the X11 window management commands you will ever need.

    You can get the window ID associated with a process ID using wmctrl in a script like this:

    #!/usr/bin/env bash
    # 
    # File:
    #   getwindidbypid
    # 
    # Description:
    #   Get the ID of a window by PID (if the process has a window).
    # 
    # Usage:
    #   getwindidbypid 
    # 
    
    while IFS= read line; do
      if [[ "${line}" =~ (0x)([0-9a-z]+)([ ][- ][0-9]+[ ])([0-9]*) ]]; then
        winId="${BASH_REMATCH[1]}${BASH_REMATCH[2]}"
        pid="${BASH_REMATCH[4]}"
        if [[ "${pid}" -eq "${1}" ]]; then
          WIND_IDS+=("${winId}")
        fi
      fi
    done < <(wmctrl -lp)
    
    if [ "${#WIND_IDS[@]}" -gt 0 ]; then
      echo "${WIND_IDS[@]}"
    fi
    

    Example:

    user ~ $  getwindidbypid 37248
    0x05a00012
    

提交回复
热议问题