Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.