Skip to main content

Time Since Last Resume

It's handy to know when to take a break.

Log the time when the system resumes from standby / sleep / hibernation

Reference: System-D Suspend Service

$ nano /usr/lib/systemd/system-sleep/sinceresume
#!/bin/sh

case $1 in
  post)
    logger "sinceresume resumed"
    ;;
esac

GrepRead the time since last resume out of the syslog

$ grep "sinceresume resumed" /var/log/syslog | tail -n 1

Read the time since start of session

$ journalctl -u systemd-logind.service | grep "New session" | tail -n 1

On Ubuntu, this ignores locking the screen. On ElementaryOS this is triggered by unlocking.

Time since last resume

$ cat ~/bin/sinceresume.sh 
#!/bin/bash

# Get the current Unix timestamp
current_unix_timestamp=$(date "+%s")


# Get the syslog entry from the command line argument
syslog_entry=resume_entry="$(grep sinceresume /var/log/syslog | tail -n 1)"
# Extract the timestamp from the syslog entry using awk
timestamp=resume_ts=$(echo "$syslog_entry"resume_entry" | awk '{print $1, $2, $3}')
# Convert the extracted timestamp to a Unix timestamp
syslog_unix_timestamp=$(date -d "$timestamp"resume_ts" "+%s")
# Get the current Unix timestamp
current_unix_timestamp=$(date "+%s")

# Calculate the number of seconds since the syslog timestamp
seconds_since_syslog=seconds_since_resume=$((current_unix_timestamp - syslog_unix_timestamp))
minutes_since_syslog=minutes_since_resume=$((seconds_since_syslogseconds_since_resume / 60))


# get time since session (elementary-OS)
session_entry=$(journalctl -u systemd-logind.service | grep "New session" | tail -n 1)
session_ts=$(echo "$minutes_since_syslogsession_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

echo "$minutes minutes since your last break ($timestamp)ts)"

I've added this to my shell startup.