You may be able to do this by configuring
logind.conf - this will also override any of your user's power settings, and will work whether you are logged in or not (system-wide). You can configure it via running:
sudo nano /etc/systemd/logind.conf
You should like something like this:
Change the below lines to this:
# at the beginning of any of the lines- a
# at the beginning of the line means it is a comment and will be ignored and the defaults will be used
This should mean that when the lid is closed the computer locks; and when it is left idle (for 20 minutes (
20x60=1200 seconds) it will enter a hybrid-sleep state - the idle action will also take place with the lid open, and the configuration file may accept the time in minutes:
Note that after making the changes you may need to restart
logind or the system to see if the changes work.
Here is a quote from the manual page - note other values like
poweroff etc can be used.
HandlePowerKey=, HandleSuspendKey=, HandleHibernateKey=, HandleLidSwitch=
Controls whether logind shall handle the system power and sleep keys and the lid switch to trigger actions such as system power-off or
suspend. Can be one of "ignore", "poweroff", "reboot", "halt",
"kexec", "suspend", "hibernate", "hybrid-sleep" and "lock". If
"ignore", logind will never handle these keys. If "lock", all running
sessions will be screen-locked; otherwise, the specified action will
be taken in the respective event. Only input devices with the
"power-switch" udev tag will be watched for key/lid switch events.
HandlePowerKey= defaults to "poweroff".
HandleLidSwitch= defaults to "suspend".
HandleHibernateKey= defaults to
Configures the action to take when the system is idle. Takes one of "ignore", "poweroff", "reboot", "halt", "kexec", "suspend",
"hibernate", "hybrid-sleep", and "lock". Defaults to "ignore".
Note that this requires that user sessions correctly report the idle status to the system. The system will execute the action after
all sessions report that they are idle, no idle inhibitor lock is
active, and subsequently, the time configured with
below) has expired.
Configures the delay after which the action configured in
IdleAction= (see above) is taken after the system is idle.