#!/bin/bash WOKE=$(expr $(journalctl -t systemd-sleep -b 0 -o json MESSAGE="System resumed." -n1 | jq -r .__REALTIME_TIMESTAMP) / 1000000) NOW=$(date +%s) UPTIME=$(expr $NOW - $WOKE) MIN_UPTIME=$(expr 60 \* 20) if [[ "$UPTIME" -lt "$MIN_UPTIME" ]] then echo "ABORT: uptime ($UPTIME s) lower than minimum ($MIN_UPTIME s)" exit 0 fi MY_SERVICE="$2" for SERVICE in $(systemctl list-timers --no-pager --no-legend --state active -o json | jq -r '.[] | .activates') do if [[ "$SERVICE" = "$MY_SERVICE" ]] then continue elif systemctl is-active "$SERVICE" --quiet then echo "ABORT: service $SERVICE is running by timer" exit 0 fi done LOGINS=$(netstat -tnpa | grep 'ESTABLISHED.*sshd' | wc -l) if [[ "$LOGINS" -gt 0 ]] then echo "ABORT: $LOGINS user logins" exit 0 fi if [[ "$1" = now ]] then echo "SESPENDING" systemctl suspend else echo "WOULD SESPEND" fi