And eventually also when they are started and closed and how many time they have been running in the background and in the foreground.
I don't need that for every single app, but only for a few. I may use a custom launcher from a desktop file (but how?). But since I have a dozen apps to track, a general solution would be better.
Thanks in advance !!
Edit : I call foreground when a window has the focus.
I'm not aware of any out of box solution.
If you're running X Window, you might easily write a shell script based on this answer. You'd have a loop where you run
xprop -root _NET_ACTIVE_WINDOW
, cut out the window id from that, then pass it toxprop -id ...
, process its output (e.g.grep
for a few relevant keys) and store it somewhere (e.g. append to a text file). Finally,sleep
for maybe 10 seconds in the loop.Meanwhile, write some scripts that process the log files (or upload that to a database, etc.).
Such approach cannot work under Wayland due to its security model, I'm afraid there you would need to hack the compositor to do this logging for you.
To check which windows has focus you can run this in a terminal:
(Notice that if you give the command
xprop...
while typing, you'll obviously have the ID of the terminal window in which you are typing...)Then you can see the PID of the process owning the window with:
...and then search the process with any of the
ps
tools.Notice that this works with X11 --- no idea about Wayland.