How to disable automatic suspend during working hours and reenable it after that automatically?

Solution 1:

You can set the suspend policy with gsettings

# no sleep on ac
gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-ac-type 'nothing'
# sleep on ac
gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-ac-type 'sleep'

# no sleep on ac
gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-battery-type 'nothing'
# sleep on ac
gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-battery-type 'sleep'

There are also options for blank (turning off the screen), shutdown, hibernate, and logout. You can further explore the options in a graphical utility called 'dconf-editor' apt install dconf-editor and navigating through the options: org > gnome > settings-daemon > plugins > power.

You could call the commands with cron, or you could use a systemd user service & timer for this. I think, while it requires more files, the systemd method is easier to setup since cron won't execute the gsettings command (which I think be a convienent way to do it) by default.

Systemd user files are stored in $HOME/.config/systemd/user. Inside this directory, you can place the two service files (one which enables sleep and the other which disables it), and their corresponding timer files.

Start the timers with:

systemctl --user enable disable_suspend.timer
systemctl --user start disable_suspend.timer

systemctl --user enable enable_suspend.timer
systemctl --user start enable_suspend.timer

Enable the services with:

systemctl --user enable disable_suspend.service

systemctl --user enable enable_suspend.service

systemd files:

(If you don't need/want to enable/disable suspend for battery you can of course remove that line in the service files.)

Contents of enable_suspend.timer which enables suspend after 6 PM:

[Unit]
Description=Timer for enabling suspend

[Timer]
OnCalendar=Mon..Fri 18:00

[Install]
WantedBy=timers.target

Contents of enable_suspend.service

[Unit]
Description=Enable Sleep

[Service]
Type=oneshot
ExecStart=gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-ac-type 'sleep'
ExecStart=gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-battery-type 'sleep'
Restart=on-failure

[Install]
WantedBy=multi-user.target

Contents of disable_suspend.timer

[Unit]
Description=Timer for disabling suspend

[Timer]
OnCalendar=Mon..Fri 9:00

[Install]
WantedBy=timers.target

Contents of disable_suspend.service

[Unit]
Description=Disable Sleep

[Service]
Type=oneshot
ExecStart=gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-ac-type 'nothing'
ExecStart=gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-battery-type 'nothing'
Restart=on-failure

[Install]
WantedBy=multi-user.target

Edit: Added the step for enabling the .service files since they got left out.