Skip to main content

Time Since Idle

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

Log the time when the user resumes from standby

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

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

Grep the time out of the syslog

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

Time since last resume

$ cat sinceresume.sh 
#!/bin/bash

# Get the syslog entry from the command line argument
syslog_entry="$(grep 'sinceidlesinceresume resumed' /var/log/syslog | tail -n 1)"

# Extract the timestamp from the syslog entry using awk
timestamp=$(echo "$syslog_entry" | awk '{print $1, $2, $3}')
echo "timestamp: $timestamp"

# Convert the extracted timestamp to a Unix timestamp
syslog_unix_timestamp=$(date -d "$timestamp" "+%s")


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

# Calculate the number of seconds since the syslog timestamp
seconds_since_syslog=$((current_unix_timestamp - syslog_unix_timestamp))

echo "Seconds since syslog timestamp: $seconds_since_syslog"