I want to execute the following task after waking up from Suspend/sleep, it shows the duration of suspend in a notify-send desktop notification.
mapfile -t UnixDate < <(journalctl -o short-iso --since "24 hours ago" | grep systemd-sleep | tail -n 2 | awk '{print $1}' | xargs -L 1 date +%s -d)
if (("${#UnixDate[@]}" == 2)); then
Duration="$(python -c "import datetime; time=(${UnixDate[1]} - ${UnixDate[0]})/3600; print(str(datetime.timedelta(hours=time)))")"
notify-send \
--app-name="Suspend" \
--icon=face-smile \
--urgency=normal \
--expire-time=3000 \
--hint int:transient:1 \
"Slept" "${Duration}"
fi
What I tried?
- Refer:
man systemd-suspend.service
- Running scripts before and after suspend with systemd – Just another Linux geek
$ pwd
/usr/lib/systemd/system-sleep
$ ls -la
total 4.0K
755 -rwxr-xr-x. 1 root root 574 2024-05-19 09:24 Suspend_notification.sh
$\cat Suspend_notification.sh
#!/usr/bin/env bash
if [ "${1}" == "post" ]; then
journalctl -o short-iso --since "24 hours ago" | grep systemd-sleep | tail -n 2
mapfile -t UnixDate < <(journalctl -o short-iso --since "24 hours ago" | grep systemd-sleep | tail -n 2 | awk '{print $1}' | xargs -L 1 date +%s -d)
if (("${#UnixDate[@]}" == 2)); then
Duration="$(python -c "import datetime; time=(${UnixDate[1]} - ${UnixDate[0]})/3600; print(str(datetime.timedelta(hours=time)))")"
notify-send \
--app-name="Suspend" \
--icon=face-smile \
--urgency=critical \
"Slept" "${Duration}"
fi
fi