Skip to content

Instantly share code, notes, and snippets.

@caarmen
Created August 5, 2024 11:27
Show Gist options
  • Save caarmen/d2aa8f6c4c953f1a0f0ad90f6ace638c to your computer and use it in GitHub Desktop.
Save caarmen/d2aa8f6c4c953f1a0f0ad90f6ace638c to your computer and use it in GitHub Desktop.
Script to monitor which application is currently opened (based on dbus-monitor).
#!/bin/bash
process_active_app() {
local last_date_str=""
local last_timestamp_s=0
local last_active_app=""
while IFS= read -r current_active_app; do
local current_timestamp
local current_date
current_timestamp=$(date +%s)
current_date=$(date +"%Y-%m-%d %H:%M:%S")
if [ $last_timestamp_s -ne 0 ]
then
local time_in_previous_app
time_in_previous_app=$((current_timestamp - last_timestamp_s))
echo "${last_date_str},${time_in_previous_app},${last_active_app}"
fi
last_active_app=${current_active_app}
last_timestamp_s=${current_timestamp}
last_date_str=${current_date}
done
}
echo "datetime_opened,duration_opened_s,application"
dbus-monitor \
| grep --line-buffered -B3 "active-on-seats" \
| grep --line-buffered "\.desktop" \
| sed --unbuffered -e 's/^ *string *"\([^"]*\)"/\1/g' \
| process_active_app
@caarmen
Copy link
Author

caarmen commented Aug 5, 2024

Tested on ubuntu using wayland.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment