Skip to main content

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):

  1. Pantheon Desktop
  2. 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

I've added this to my shell startup.