You are not logged in.

#1 2024-02-17 21:19:15

andyturfer
Member
Registered: 2021-01-08
Posts: 90

[SOLVED] Bash script to put laptop to sleep

I have a small issue (annoyance) with my laptop - if the laptop is sleeping with the lid open, and the lid is then closed, the laptop wakes up.

I'm trying to implement a dirty workaround where a bash script tests if the laptop lid is closed, and then calls systemctl suspend if it is.

In /usr/lib/systemd/system-sleep I have created onwakeup.sh:

#!/bin/bash
PATH=/sbin:/usr/sbin:/bin:/usr/bin

if [[ "${1}" == "post" ]]; then
    # AFTER RESUMING FROM SLEEP
    printf "`date +%T` " >> /tmp/wake.log
    echo "Have resumed..." >> /tmp/wake.log
    sleep 1
    grep -q closed /proc/acpi/button/lid/LID0/state
    if [ $? = 0 ]
    then
        printf "`date +%T` " >> /tmp/wake.log
        echo "Have resumed and lid is closed, attempting to suspend..." >> /tmp/wake.log
        systemctl suspend
    fi
fi

The systemctl suspend line doesn't work though. From /tmp/wake.log:

18:39:50 Have resumed...
18:39:51 Have resumed and lid is closed, attempting to suspend...

Any idea why the systemctl suspend line in the script isn't working?

Last edited by andyturfer (2024-02-18 00:10:34)

Offline

#2 2024-02-17 21:57:28

seth
Member
Registered: 2012-09-03
Posts: 58,659

Offline

#3 2024-02-18 00:10:05

andyturfer
Member
Registered: 2021-01-08
Posts: 90

Re: [SOLVED] Bash script to put laptop to sleep

Solved: https://bbs.archlinux.org/viewtopic.php … 5#p2151355

Looks like systemctl suspend doesn't work for scripts in /usr/lib/systemd/system-sleep. Had to use this instead:

echo freeze > /sys/power/state

Offline

Board footer

Powered by FluxBB