4

Using Is there a "variable" to see if computer is in sleep mode?

Here is the code that should output to a file when system goes into SUSPEND or AWAKES:

(this code is in /etc/pm/sleep.d)

(also had to make the file executable: sudo chmod +x sleep_mode)

(when running from the command line, the "suspend script" is written to the file.

(but when I suspend the computer or awaken the computer... nothing is written to file.)

#!/bin/bash

# general entry 
echo "suspend script"
echo "%suspend script" >>  /tmp/suspend_time.txt
date +%s >>  /tmp/suspend_time.txt

case "$1" in
    suspend)
        # executed on suspend
        echo "%system_suspend" >> /tmp/suspend_time.txt
        date +%s >> /tmp/suspend_time.txt
        ;;
    resume) 
        # executed on resume
        echo "%system_resume" >> /tmp/suspend_time.txt
        date +%s >> /tmp/suspend_time.txt
        ;;
    *)
        ;;
esac

1 Answer 1

4

Copy your script to:

/lib/systemd/system-sleep/sleep_mode

You will need to use sudo powers. After copying flag it as executable:

sudo chmod +x /lib/systemd/system-sleep/sleep_mode

Additionally change all occurrences of:

echo "%s

to:

echo "s

The percent sign is unnecessary.

The existing date command is OK:

date +%s >>  /tmp/suspend_time.txt

However it is formatted as number of seconds since January 1, 1970 which isn't the most readable date format.


case statement

The case statement can be changed:

case $1/$2 in
  pre/*)
    echo "$0: Going to $2..."
    # Place your pre suspend commands here, or `exit 0` if no pre suspend action required
    ;;
  post/*)
    echo "$0: Waking up from $2..."
    # Place your post suspend (resume) commands here, or `exit 0` if no pre suspend action required
    ;;
esac
3
  • made the modifications... it now enters the script on system SLEEP and AWAKE... but it does not execute either of the cases.
    – jdl
    Dec 20, 2020 at 21:26
  • @jdl I've added a new section to the answer with a different case statement. Dec 20, 2020 at 21:32
  • works now... thx
    – jdl
    Dec 20, 2020 at 21:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .