First, I created a new file /etc/acpi/events/laptop-lid
containing the following:
event=button/lid.*
action=/etc/acpi/laptop-lid.sh
Then I created /etc/acpi/laptop-lid.sh
containing this code:
#!/bin/bash
source /home/michael/.Xdbus
grep -q closed /proc/acpi/button/lid/LID/state
if [ $? = 0 ]
then
# close action
dm-tool switch-to-greeter
sleep 5
systemctl suspend
else
# open action
fi
In /home/michael/.Xdbus
I put the following:
export XDG_SEAT_PATH=/org/freedesktop/DisplayManager/Seat0
And I made /etc/acpi/laptop-lid.sh
executable (chmod a+x
).
Then I ran
/etc/init.d/acpid restart
Finally, I went into XFCE Power Manager and set it to "Switch off display" when the laptop lid is closed, so as to avoid conflicting with my new, improved functionality.
However, I found there was still a problem - after I came out of suspend, the login screen would be displayed and then the machine would suspend again. After a bit of searching I think this is due to a bug in Ubuntu that apparently still exists in 18.04. I worked around it by checking if more than 6 seconds have passed since I tried to sleep 5 seconds, and if so I don't suspend. It seems like my laptop actually suspends in under 2 seconds, so this is always working for me. I tried setting Xfce to lock the screen, but it didn't seem to honor that either - in both cases, my machine was suspended in under 2 seconds, whereas my script should be forcing it to wait at least 5.
Update:
After doing all of this, I was still totally unsatisfied. Because my XFCE Power Manager settings are basically ignored leading the machine to always sleep when I close the lid, this creates a race condition between the sleep process and how far the dm-tool gets towards creating the greeter, which always results in some delay on resume while that process finishes bringing up the greeter. So I totally undid all of the above work and instead did the following:
Edit ~/.config/openbox/lubuntu-rc.xml
. Find the key binding to lock the screen and change it to switch to the greeter instead. The main effect of this seems to be to not turn off the display, which seems important to making the greeter show up instantly on resume:
<keybind key='W-L'>
<action name='Execute'>
<command>dm-tool switch-to-greeter</command>
</action>
</keybind>
Now instead of closing the lid, I hit Win+L, and as soon as the greeter appears I then close the lid.
Note: I wanted to have a separate script that first switches to the greeter, then suspends, but it seems like no matter how I try to suspend (systemctl, dbus, etc.) they all refuse to suspend from the lock screen without manual intervention - entering a password from a dialog box - that impossible to satisfy because we are at the lock screen!
Best Answer
Using
inoticoming
:You can put a script in
/etc/init.d/
that runsinoticoming
at boot time.Create a new folder to hold the
inoticoming
log / lastpid
for thewatched
folder:sudo mkdir -p /var/log/inoticoming/watched/
Create a script
inoticoming_watched
in/etc/init.d/
:* Remember to change <path_to_folder> and <path_to_script> to match the full path of the
watched
folder and the full path of the script to executeMark the script as executable:
sudo chmod u+x /etc/init.d/inoticoming_watched
Make sure that the script called by
inoticoming_watched
is executable.Update
rc.d
to make the serviceinoticoming_watched
start at boot time:sudo update-rc.d inoticoming_watched defaults
You can check the
inoticoming
log in/var/log/inoticoming/watched
.