Run command after resume from sleep state

I am trying to run commands after my computer resumes.

This does not do anything. ?

It does not create a file when run manually or part of resume.

#!/bin/sh
#
# Created 3/31/19 in /usr/lib/systemd/system-sleep/
if [ “${1}” = “pre” ]; then
# Do the thing you want before suspend here
echo “we are suspending at $(date)…” > /home/andy/Downloads/systemd_suspend_test
elif [ “${1}” = “post” ]; then
# Do the thing you want after resume here
echo “…and we are back from $(date)” >> /home/andy/Downloads/systemd_suspend_test
/usr/bin/gxmessage -fg red -font ‘sans 30’ -timeout 3 ‘ Computer has now resumed from suspend state.’
fi

https://blog.christophersmart.com/2016/05/11/running-scripts-before-and-after-suspend-with-systemd/

I tried this on my laptop, closed lid, re-opened lid, pressed power to resume, got a file /tmp/systemd_suspend_test

Even when I ran manually ...

# /lib/systemd/system-sleep/useless pre
# more /tmp/systemd_suspend_test 
we are suspending at Mon Apr  1 12:43:33 MST 2019...
# /lib/systemd/system-sleep/useless post
# more /tmp/systemd_suspend_test 
we are suspending at Mon Apr  1 12:43:33 MST 2019...
...and we are back from Mon Apr  1 12:44:00 MST 2019

your script is named "On_Resume.sh" just change it to useless (no extension) and try ...

I removed the extension.

No change. :rage:

are the permissions 755 for the script?

Is that the same as being executable?

I used chmod +x

ls -l /lib/systemd/system-sleep/useless

-rwxr-xr-x 1 root root 466 Apr 1 14:33 useless

Yep, the same as mine.

systemctl status systemd-suspend.service

So, what is it telling me?

systemctl status systemd-suspend.service
● systemd-suspend.service - Suspend
Loaded: loaded (/lib/systemd/system/systemd-suspend.service; static; vendor p
Active: inactive (dead)
Docs: man:systemd-suspend.service(8)

Apr 01 18:20:47 7 systemd-sleep[30212]: /lib/systemd/system-sleep/On_Resume.sh:
Apr 01 18:20:47 7 systemd-sleep[30212]: /lib/systemd/system-sleep/On_Resume.sh:
Apr 01 18:20:47 7 systemd-sleep[30212]: Suspending system...
Apr 01 19:48:05 7 systemd-sleep[30212]: System resumed.
Apr 01 19:48:05 7 systemd-sleep[30212]: /lib/systemd/system-sleep/On_Resume.sh:
Apr 01 19:48:05 7 systemd-sleep[30212]: /lib/systemd/system-sleep/On_Resume.sh:
Apr 01 19:48:18 7 systemd-sleep[30212]: /dev/sda:
Apr 01 19:48:18 7 systemd-sleep[30212]: setting Advanced Power Management level
Apr 01 19:48:18 7 systemd-sleep[30212]: APM_level = 254
lines 1-14...skipping...
● systemd-suspend.service - Suspend
Loaded: loaded (/lib/systemd/system/systemd-suspend.service; static; vendor preset: enabled)
Active: inactive (dead)
Docs: man:systemd-suspend.service(8)

Apr 01 18:20:47 7 systemd-sleep[30212]: /lib/systemd/system-sleep/On_Resume.sh: 2: [: pre: unexpected operator
Apr 01 18:20:47 7 systemd-sleep[30212]: /lib/systemd/system-sleep/On_Resume.sh: 5: [: pre: unexpected operator
Apr 01 18:20:47 7 systemd-sleep[30212]: Suspending system...
Apr 01 19:48:05 7 systemd-sleep[30212]: System resumed.
Apr 01 19:48:05 7 systemd-sleep[30212]: /lib/systemd/system-sleep/On_Resume.sh: 2: [: post: unexpected operator
Apr 01 19:48:05 7 systemd-sleep[30212]: /lib/systemd/system-sleep/On_Resume.sh: 5: [: post: unexpected operator
Apr 01 19:48:18 7 systemd-sleep[30212]: /dev/sda:
Apr 01 19:48:18 7 systemd-sleep[30212]: setting Advanced Power Management level to 0xfe (254)
Apr 01 19:48:18 7 systemd-sleep[30212]: APM_level = 254
Apr 01 19:48:18 7 systemd[1]: Started Suspend.

It tells you that the suspend.service is loaded. But, you should not be getting errors at 18:20 (unexpected operator). I thought you changed the On_Resume.sh script to On_Resume
We checked that its permissions are 755, my script works as expected.

My /lib/systemd/system-sleep/useless script

#!/bin/sh
if [ "${1}" = "pre" ]; then
  # Do the thing you want before suspend here, e.g.:
  echo "we are suspending at $(date)..." > /home/pavlos/systemd_suspend_test
elif [ "${1}" = "post" ]; then
  # Do the thing you want after resume here, e.g.:
  echo "...and we are back from $(date)" >> /home/pavlos/systemd_suspend_test
fi

Testing ...
$ /lib/systemd/system-sleep/useless pre
wait a bit ...
$ /lib/systemd/system-sleep/useless post

$ more /home/pavlos/systemd_suspend_test
we are suspending at Mon Apr 1 18:20:52 MST 2019...
...and we are back from Mon Apr 1 18:20:56 MST 2019

Similar event when I close the lid to suspend my laptop and then resume it.

Cannot figure out why your script does not work...

I did get rid of .sh extension.

Those other entries were from earlier in the day.

I activate suspend using either my sleep key or systemctl suspend.

Try a different shebang. Maybe #!/bin/bash.