0

Following https://wiki.archlinux.org/index.php/Power_management#Hooks_in_/usr/lib/systemd/system-sleep, I add a shell script as a systemd sleep hook with execution permission (I manually created /usr/lib/systemd/system-sleep/ which didn't exist by default):

$ ls /usr/lib/systemd/system-sleep/ -l
total 4
-rwxr-xr-x 1 root root 322 Dec  2 19:28 systemd_suspend_cpu_freq.sh

whose content is

#!/bin/sh

case "$1/$2" in
    post/*)
    echo "systemd_suspend hook" >>/tmp/mylog
    ;;
esac

I first suspend my Lubuntu 18.04, by

$ systemctl suspend 
User testme is logged in on seat0.
User testme is logged in on sshd.
User testme is logged in on seat0.
User testme is logged in on seat0.
Please retry operation after closing inhibitors and logging out other users.
Alternatively, ignore inhibitors and users with 'systemctl suspend -i'.
$ systemctl suspend  -i
$

Then I wake it up, and I don't find anything in /tmp/mylog supposed to be written by my script, and also

$ journalctl -b -u systemd-suspend.service

-- Logs begin at Mon 2018-11-12 13:25:27 EST, end at Sun 2018-12-02 19:30:04 EST. --
Dec 02 18:38:05 ocean systemd[1]: Starting Suspend...
Dec 02 18:38:05 ocean systemd-sleep[17888]: Suspending system...
Dec 02 18:51:16 ocean systemd[1]: Started Suspend.
Dec 02 19:29:30 ocean systemd[1]: Starting Suspend...
Dec 02 19:29:30 ocean systemd-sleep[20436]: Suspending system...
Dec 02 19:29:42 ocean systemd-sleep[20436]: /dev/sda:
Dec 02 19:29:42 ocean systemd-sleep[20436]:  setting Advanced Power Management level to 0xfe (254)
Dec 02 19:29:42 ocean systemd-sleep[20436]:  APM_level        = 254
Dec 02 19:29:35 ocean systemd-sleep[20436]: System resumed.
Dec 02 19:29:42 ocean systemd[1]: Started Suspend.

I was wondering why my script isn't executed when waking up from suspension?

Thanks.


Update

The following suggestions in the comment doesn't make the hook execute upon waking up:

  • mkdir /home/t/tmpdir; chmod 777 /home/t/tmpdir
  • redirection to /home/t/tmpdir/mylog in the script (no file was created under /home/t/tmpdir/)
Tim
  • 101,790
  • Try writing to another directory e.g create /home/tim/tmpdir and chmod 777 it and write to there. Many services under systemd have "private" /tmp directories. – Stephen Harris Dec 03 '18 at 00:52
  • You edits did not do what I said. mkdir /home/tim/tmpdir and chmod 777 /home/tmp/tmpdir (or whatever your home directory is) and then write to that. /tmp/test/mylog is a subdirectory of /tmp and so may be part of a private /tmp tree. – Stephen Harris Dec 03 '18 at 01:06
  • OK, good. So those changes now rule out a potential confounding factor, and so make the question clearer. I don't know why you're following an ArchLinux procedure when you're running a Ubuntu derivative, but at least we've cleared up one possible failure mode. – Stephen Harris Dec 03 '18 at 01:18
  • 2
    And now you get a downvote because your question was based on "why doesn't /tmp/mylog" appear, when you had additional knowledge. I don't appreciate having my time wasted on partial questions and partial knowlege. – Stephen Harris Dec 03 '18 at 01:32
  • Tim, you make it really really hard for people to even want to help you. You've just received the equivalent of many $$$ in consulting, and then attack the person trying to help you because you didn't provide the right information up front. – Stephen Harris Dec 03 '18 at 01:39
  • @StephenHarris the Arch wiki is probably the best available documentation in the Linux world. There is nothing strange about following it when using a different distribution. The instructions on the page the OP linked to should work on any distribution that's using systemd. And claiming your comments are worth "many $$$" seems a bit far fetched, wouldn't you say? All you did is suggest the OP try a different tmp dir. Useful suggestion, mind you, but come on! – terdon Dec 03 '18 at 10:54
  • @tim can you clarify what happens when you redirect to /home/tim/tmpdir? You say the change Stephen suggested doesn't make the hook ruin (it wasn't supposed to). Does that mean that even when writing to /home/tim/tmpdir you see no file created after waking up? – terdon Dec 03 '18 at 10:56
  • @terdon review the edit history and you'll see where that came from. – Stephen Harris Dec 03 '18 at 11:40
  • @StephenHarris It won't be easy to tell which happens after which. At first, you downvoted my post. A while later, asked me to try. I tried. You corrected me, I tried again, and reported and mentioned it was not surprising because of unmentioned evidence in actual script, which is not essential to be included and I wanted to keep my question minimal. When I saw your comment, my first reaction was focusing on changing redirection destination didn't make sense (I was not questioning your qualification) even without the additional evidence, but i might not know so I tried. – Tim Dec 03 '18 at 11:45
  • you started blaming me for not telling you unmentioned, for wasting your time, comparing wasting time here to your profitable consulting business. Calling my reaction in my post update as attacking you. – Tim Dec 03 '18 at 11:51
  • I have been aware of your history of treating my posts unfairly, which also has encouraged your alikes to do the same. I avoided thinking about that when you tried to guide me here, because I thought it was based on good faith. – Tim Dec 03 '18 at 12:06

1 Answers1

2

I ran into the same error. The arch linux wiki is currently incorrect in regard where to put the script. You need to put the script into /lib/systemd/system-sleep/ instead of /usr/lib/systemd/system-sleep/.

See also man systemd-suspend.service:

Immediately before entering system suspend and/or hibernation systemd-suspend.service (and the other mentioned units, respectively) will run all executables in /lib/systemd/system-sleep/ and pass two arguments to them.

rednammoc
  • 121