Initial commit

This commit is contained in:
2025-07-02 10:35:14 +02:00
commit 9445b1ff44
5 changed files with 93 additions and 0 deletions

21
README.md Normal file
View File

@@ -0,0 +1,21 @@
# SmartSleep
Ein schlankes Skript zur Steuerung des Energiesparmodus
## Beschreibung
Das Projekt besteht aus zwei Hauptkomponenten:
### `smartsleep.sh`
Wird bei Ein- und Aufwachvorgängen des Systems ausgeführt.
- Protokolliert Systemzustände (Eintritt/Verlassen des Energiesparmodus)
- Ermittelt den Zustand von Laptop-Deckel und angeschlossener Dockingstation
- Wartet auf Druck der STRG-Taste während des Aufwachvorgangs
- Steuert den Daemon über Signale (USR1/USR2)
### `smartsleepd`
Läuft im Hintergrund und verwaltet Aufwach-Logik
- Empfängt Signale von `smartsleep.sh`
- Wechselt erneut in den Schlafmodus, wenn keine STRG-Taste gedrückt wurde

6
deploy Executable file
View File

@@ -0,0 +1,6 @@
#!/bin/bash
sudo cp smartsleep.sh /usr/lib/systemd/system-sleep/
pkill smartsleepd
./smartsleepd &
pgrep smartsleepd

5
log Executable file
View File

@@ -0,0 +1,5 @@
#!/bin/bash
journalctl -b | grep -i "systemd-sleep\|sleep\.target" | tail -n 25
echo
journalctl -t "SmartSleep" | tail -n 30

38
smartsleep.sh Executable file
View File

@@ -0,0 +1,38 @@
#!/bin/bash
TIMEOUT_STRG=5 # Zeit in Sekunden die bleibt, um STRG zu drücken
USER=$(who | awk '/tty7|:0/ {print $1; exit 0}')
[ -n "$USER" ] || exit 0
export DISPLAY=:0
export XAUTHORITY=/home/$USER/.Xauthority
case $1 in
pre)
logger -t "SmartSleep" "Das System wechselt in den Energiesparmodus"
[ -n "$(pgrep smartsleepd)" ] || logger -t "SmartSleep" -p "user.warning" "Der Daemon läuft nicht!"
;;
post)
LID_STATUS="$( awk '{print $2}' < /proc/acpi/button/lid/LID0/state )" # open|closed
DOCK_STATUS="$( lsusb | grep -iq "Dell dock" && echo "yes" || echo "no" )"
if [ "$LID_STATUS" = "open" ] || [ "$DOCK_STATUS" = "yes" ]; then
timeout $TIMEOUT_STRG bash -c '
while true; do
for n in $( xinput --list | grep keyboard | grep -Po "id=[0-9]+" | cut -c4- ); do
if xinput --query-state $n 2> /dev/null | grep down | grep -Eq "(\[37\]|\[105\])"; then
exit 1
fi
done
done'
fi
if [ "$?" -eq 1 ]; then
logger -t "SmartSleep" "Benutzerinteraktion (STRG-Taste)"
kill -USR2 $(pgrep -f smartsleepd)
else
logger -t "SmartSleep" -p "user.warning" "Keine Benutzerinteraktion (STRG-Taste)."
kill -USR1 $(pgrep -f smartsleepd)
fi
;;
esac

23
smartsleepd Executable file
View File

@@ -0,0 +1,23 @@
#!/bin/bash
checkSleep() {
if timeout 15 bash -c '
until journalctl -b | grep -i "systemd-sleep\|sleep\.target" | tail -n1 | grep -iq "Stopped target"; do
sleep 0.5
done';
then
logger -t "SmartSleep" "Das System hat den Energiesparmodus beendet"
return 0
else
logger -t "SmartSleep" "Das System hat den Energiesparmodus nicht wie erwartet beendet"
return 1
fi
}
trap 'checkSleep && systemctl suspend' USR1
trap 'notify-send "SmartSleep" "Energiesparmodus beendet"; checkSleep' USR2
while true; do
sleep infinity &
wait $!
done