Sample Header Ad - 728x90

Execute a task after waking up from Suspend/sleep

0 votes
1 answer
196 views
# Info Fedora 40, Gnome 46, Wayland
systemctl --version
systemd 255 (255.6-1.fc40)

notify-send --version
notify-send 0.8.3
# Task 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} - ${UnixDate})/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](https://blog.christophersmart.com/2016/05/11/running-scripts-before-and-after-suspend-with-systemd/)
$ 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} - ${UnixDate})/3600; print(str(datetime.timedelta(hours=time)))")"
                notify-send \
                        --app-name="Suspend" \
                        --icon=face-smile \
                        --urgency=critical \
                        "Slept" "${Duration}"
        fi
fi
Asked by Porcupine (2156 rep)
May 21, 2024, 03:57 PM
Last activity: May 22, 2024, 01:32 PM