The default behavior of Ubuntu 18.04 is not friendly for a notebook that swaps back and forth from desk to mobile.
Because I forget things, I have to write them down. And sometimes, that is just noting where I found a good answer to a problem I was having.
Background
I re-installed Ubuntu and out of the box it suspends the host when the lid is closed. That’s great if you are mobile but terrible if you use the notebook on a KVM or just as a build machine or such sitting on the network.
What we want to meet both cases is a way to suspend on lid close UNLESS the notebook is plugged into power.
Acknowledgements - This Work Is Not Mine!
This solution came from here. But since sometimes sites go away I’m going to duplicate the content below.
Solution from MacieJ Mensfeld
You need to be root.
#sudo -i # really, you need to be root
echo 'HandleLidSwitch=ignore' | tee --append /etc/systemd/logind.conf
echo 'HandleLidSwitchDocked=ignore' | tee --append /etc/systemd/logind.conf
service systemd-logind restart
echo 'event=button/lid.*' | tee --append /etc/acpi/events/lm_lid
echo 'action=https://blog.herlein.com/etc/acpi/lid.sh' | tee --append /etc/acpi/events/lm_lid
touch /etc/acpi/lid.sh
chmod +x /etc/acpi/lid.sh
cat > /etc/acpi/lid.sh << EOF
#!/bin/bash
USER=your_username
grep -q close /proc/acpi/button/lid/*/state
if [ $? = 0 ]; then
su -c "sleep 1 && xset -display :0.0 dpms force off" - $USER
fi
grep -q open /proc/acpi/button/lid/*/state
if [ $? = 0 ]; then
su -c "xset -display :0 dpms force on &> /tmp/screen.lid" - $USER
fi
EOF
Disclaimer
This works for me. Your results may vary.