Skip to content

Instantly share code, notes, and snippets.

@markcaudill
Created June 22, 2019 19:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markcaudill/c78441540ee795a979d82cca1ddd2478 to your computer and use it in GitHub Desktop.
Save markcaudill/c78441540ee795a979d82cca1ddd2478 to your computer and use it in GitHub Desktop.
Linux App Usage Tracker
#!/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