Directly from the manual for 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 /usr/lib/systemd/system-sleep/ and pass two arguments to them. The first argument will be "pre", the second either "suspend", "hibernate", or "hybrid-sleep" depending on the chosen action. Immediately after leaving system suspend and/or hibernation the same executables are run, but the first argument is now "post". All executables in this directory are executed in parallel, and execution of the action is not continued until all executables have finished.
I did some testing, and found that if you put a script (marked executable by all, which be achieved with chmod a+x
) in /lib/systemd/system-sleep
, without the /usr, it will be executed as mentioned.
However, there is one drawback which is what makes you think it isn't working: the environment is different. Graphical commands do not work. There is probably a way around this, but by default, unity --replace
will fail because it can't connect to the X server (and so no commands that don't work if you press CTRL-ALT-F1 and go into an actual console without tampering will function).
Okay, I just paused my typing to do some research: after looking at the basic chroot wiki (because I've had the same problem in chroots), I found that if you manually call xhost +
before suspending, graphical commands (in my case zenity --info --text "1:$1 2:$1"
) function properly. However, you can't call xhost +
from the script. What you can do is add a program to the "startup applications" to automatically call xhost +
when you log in (add it there and not to .bashrc
because if added to .bashrc
it will run every time you open a terminal emulator). It might be different on Unity, but on Ubuntu MATE I can go to System → Preferences → Personal → Startup Applications
and click Add to do this.
Then, create a file /lib/systemd/system-sleep/restart-unity
with the following contents:
#!/bin/bash
export DISPLAY=:0
if [[ "$1" == "post" && "$2" == "suspend" ]]
then
sleep 5
unity --replace
fi
and run sudo chmod a+x /lib/systemd/system-sleep/restart-unity
(make sure the file is owned by root and only writable by root).
Note that it must use bash
. This is because of the "if" statement. When I tried the statement manually in sh
, I got various errors from the &&
part.
The sleep
is there because without it, unity --replace
will fail to connect to the X server (or so it seems).
Based on my own tests on Zesty, this should work.
I just spent 15 minutes trying to figure out why it wasn't working only to realize I'd forgotten the export DISPLAY=:0
line.