Created
June 22, 2019 19:55
-
-
Save markcaudill/c78441540ee795a979d82cca1ddd2478 to your computer and use it in GitHub Desktop.
Linux App Usage Tracker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
DATABASE=${HOME}/.app-track.db | |
touch "${DATABASE}" && chmod 0600 "${DATABASE}" | |
active_window_id() { | |
xprop -root _NET_ACTIVE_WINDOW | rev | cut -d' ' -f1 | rev | |
} | |
window_pid_by_id() { | |
xprop -id ${1} _NET_WM_PID | rev | cut -d' ' -f1 | rev | |
} | |
process_name_by_pid() { | |
basename $(ps -q "${1}" -o command=) | |
} | |
active_window_process() { | |
id="$(active_window_id)" | |
if [ "${id}" = "0x0" ]; then | |
echo "Desktop" | |
else | |
process_name_by_pid "$(window_pid_by_id "${id}")" | |
fi | |
} | |
timestamp() { | |
date '+%s' | |
} | |
latest_entry() { | |
tail -1 "${DATABASE}" | |
} | |
latest_process() { | |
echo "$(latest_entry)" | cut -d',' -f2 | |
} | |
add_entry() { | |
echo "$(timestamp),${*}" >> "${DATABASE}" | |
} | |
while [ 1 ]; do | |
t=$(timestamp) | |
p="$(active_window_process)" | |
[ "${p}" = "$(latest_process)" ] || add_entry "${p}" | |
sleep 1 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment