| Server IP : 3.147.158.171 / Your IP : 216.73.216.229 Web Server : Apache/2.4.67 (Amazon Linux) OpenSSL/3.5.5 System : Linux ip-172-31-2-178.us-east-2.compute.internal 6.1.172-216.329.amzn2023.x86_64 #1 SMP PREEMPT_DYNAMIC Wed May 20 06:31:34 UTC 2026 x86_64 User : ec2-user ( 1000) PHP Version : 8.4.21 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /etc/acpi/actions/ |
Upload File : |
#!/usr/bin/bash
PATH=/sbin:/bin:/usr/bin
# Hibernation selects the swapfile with highest priority. Since there may be
# other swapfiles configured, ensure /swap is selected as hibernation
# target by setting to maximum priority.
swap_priority=32767
LOCKFILE=/var/run/hibernate.lock
HIBERNATE_STATE_DIR=/var/run/hibernate
HIBERNATED_AT="$HIBERNATE_STATE_DIR/hibernated_at"
RESUMED_AT="$HIBERNATE_STATE_DIR/resumed_at"
STALE_THRESHOLD=30
LOGNAME="hibernate"
log() {
local level="$1"
local message="$2"
logger -t "$LOGNAME" -p "user.$level" "[PID $$] $message"
}
should_hibernate() {
local now=$(date +%s)
if [ -f "$RESUMED_AT" ]; then
local resumed=$(cat "$RESUMED_AT")
local since_resume=$((now - resumed))
if [ $since_resume -lt $STALE_THRESHOLD ]; then
log notice "Resumed ${since_resume}s ago, skipping"
return 1
fi
return 0
fi
if [ -f "$HIBERNATED_AT" ]; then
local hibernated=$(cat "$HIBERNATED_AT")
local since_hibernate=$((now - hibernated))
if [ $since_hibernate -lt $STALE_THRESHOLD ]; then
log notice "Hibernation started ${since_hibernate}s ago, skipping"
return 1
fi
log notice "Stale hibernated_at (${since_hibernate}s old), clearing"
rm -f "$HIBERNATED_AT"
return 0
fi
return 0
}
do_hibernate() {
local event="$1"
for i in 1 2 3; do
log notice "Attempt $i/3: Enabling swap"
if ! swapon --priority=$swap_priority /swap; then
log err "Attempt $i/3: Failed to enable swap, retrying in 10s"
sleep 10
continue
fi
log notice "Attempt $i/3: Swap enabled, initiating hibernation"
sleep 1
if systemctl hibernate; then
log notice "Hibernate initiated"
return 0
else
log err "Attempt $i/3: Hibernation initiation failed, disabling swap, retrying in 10s"
swapoff /swap
sleep 10
fi
done
log err "All hibernation attempts failed"
rm -f "$HIBERNATED_AT"
return 1
}
mkdir -p "$HIBERNATE_STATE_DIR"
case "$2" in
LNXSLPBN:*|SBTN)
if ! should_hibernate; then
log notice "ACPI event $2 ignored"
exit 0
fi
rm -f "$RESUMED_AT"
hibernated_time=$(date +%s)
log notice "Hibernation requested at $hibernated_time"
echo "$hibernated_time" > "$HIBERNATED_AT"
exec 200>"$LOCKFILE"
if flock -n 200; then
log notice "ACPI event $2 received, initiating hibernation"
do_hibernate "$2"
else
log notice "ACPI event $2 ignored, hibernation already in progress"
fi
;;
*)
log warning "Unknown ACPI event: $2"
;;
esac