0

You can list the process ID of each widow with this command:

wmctrl -lp

Does there exist a command that shows the running command of each window (kind of like htop has a column for "Command")?

If not, how could you combine commands to ultimately achieve this?

2 Answers 2

2

This will replace the pid in wmctrl -lp’s output with the corresponding command, if one is found:

wmctrl -lp | awk '{ pid=$3; cmd="ps -o comm= " pid; while ((cmd | getline command) > 0) { sub(" " pid " ", " " command " ") }; close(cmd) } 1'

This obviously won’t work for windows displaying remote processes; it also will give strange results for windows corresponding to sandboxed processes in some cases (e.g. Flatpak).

The AWK script reads each line, extracts the pid, and runs ps -o comm= to determine the corresponding command; if one is found, it replaces the corresponding pid string with the command.

2
  • 1
    nooooice; this is very pretty. Commented Dec 22, 2021 at 15:36
  • I've got to learn awk! Commented Dec 22, 2021 at 15:52
2

wow, learned about wmctrl today.

Well, this is incredibly close already! Simply take these PIDs, and check their command:

for pid in $(wmctrl -lp | tr -s " "| cut -d ' ' -f3); do
#^--|------|--------------|-----------|------------- for .. in .. loop
#   |      |              |           |              
#   \------|--------------|-----------|------------- name of the variable we'll set
#          |              |           |              each iteration
#          |              |           |              
#          \--------------|-----------|------------- $(command): replaces $(..) 
#                         |           |              with output of `command`
#                         |           |              
#                         \-----------|------------- translate character " " by -s:
#                                     |               "squeeze" multiple consecutive 
#                                     |               spaces into one
#                                     |               
#                                     \-------------- cut at ' ', take the 3rd field
  cat "/proc/${pid}/cmdline"
  echo ""
done

Also interesting, mabye:

for wm_id in $(wmctrl -l | cut -d ' ' -f1); do
  xprop -id "${wm_id}" WM_CLASS
done
1
  • I really love the way you commented that command; I've saved it into my notes so I can model after it. Have a Merry Christmas. I recorded this today, just for you. Commented Dec 22, 2021 at 21:47

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.