Time Since Last Resume
It's handy to know when to take a break:
➜ ~ sinceresume.sh 0
14 minutes since your last break (Sep 13 13:56:49)
Supported OSes (patches welcome):
- Pantheon Desktop
- Ubuntu Desktop
Log the time when the system resumes from standby / sleep / hibernation
Reference: System-D Suspend Service
$ sudo nano /usr/lib/systemd/system-sleep/sinceresume
#!/bin/sh
case $1 in
post)
logger "sinceresume resumed"
;;
esac
Time since last resume
$ cat ~/bin/sinceresume.sh
#!/usr/bin/env bash
# Set display threshold to the user-provided value or default to 20 minutes.
threshold="${1:-20}"
# Get the current Unix timestamp
current_unix_timestamp=$(date "+%s")
# Get the syslog entry from the command line argument
resume_entry="$(grep 'sinceresume resumed' /var/log/syslog | tail -n 1)"
# Extract the timestamp from the syslog entry using awk
resume_ts=$(echo "$resume_entry" | awk '{print $1, $2, $3}')
# Convert the extracted timestamp to a Unix timestamp
syslog_unix_timestamp=$(date -d "$resume_ts" "+%s")
seconds_since_resume=$((current_unix_timestamp - syslog_unix_timestamp))
minutes_since_resume=$((seconds_since_resume / 60))
# get time since session / last unlock
case "$XDG_SESSION_DESKTOP" in
ubuntu)
session_entry=$(journalctl -u session-2.scope | grep "gkr-pam: unlocked login keyring" | tail -n 1)
;;
pantheon)
session_entry=$(journalctl -u systemd-logind.service | grep "New session" | tail -n 1)
;;
*)
echo "Unsupported XDG_SESSION_DESKTOP $XDG_SESSION_DESKTOP!"
exit 1
;;
esac
session_ts=$(echo "$session_entry" | awk '{print $1, $2, $3}')
# Convert the extracted timestamp to a Unix timestamp
syslog_unix_timestamp=$(date -d "$session_ts" "+%s")
seconds_since_session=$((current_unix_timestamp - syslog_unix_timestamp))
minutes_since_session=$((seconds_since_session / 60))
if [ "$minutes_since_resume" -lt "$minutes_since_session" ]; then
minutes="$minutes_since_resume"
ts="$resume_ts"
else
minutes="$minutes_since_session"
ts="$session_ts"
fi
if [ "$minutes" -gt "$threshold" ]; then
echo "$minutes minutes since your last break ($ts)"
fi
Going forward I maintain the fish version:
#!/usr/bin/env fish
set display_threshold (count $argv > 0; and math "$argv[1]"; or echo 20)
set current_unix_timestamp (date "+%s")
# Get time since last sleep / suspend / hibernate
set resume_entry (grep sinceresume /var/log/syslog | tail -n 1)
set resume_date (echo "$resume_entry" | awk '{print $1, $2, $3}')
set resume_ts (date -d "$resume_date" "+%s")
set minutes_since_resume (math "($current_unix_timestamp - $resume_ts) / 60")
# Get time since session / last unlock
switch "$XDG_SESSION_DESKTOP"
case ubuntu
set session_entry (journalctl -u session-2.scope | grep "gkr-pam: unlocked login keyring" | tail -n 1)
case pantheon
set session_entry (journalctl -u systemd-logind.service | grep "New session" | tail -n 1)
case '*'
echo "Unsupported XDG_SESSION_DESKTOP $XDG_SESSION_DESKTOP!"
exit 1
end
set session_date (echo "$session_entry" | awk '{print $1, $2, $3}')
set session_ts (date -d "$session_date" "+%s")
set minutes_since_session (math "($current_unix_timestamp - $session_ts) / 60")
set minutes (math floor (math min $minutes_since_session,$minutes_since_resume ))
if test $minutes -gt $display_threshold
echo "$minutes"m
end
I've added this to my shell startup.