Score:0

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

ch flag

I want to automatically disable automatic suspend during working hours (Mon - Fri between 9 am and 6 pm) and re-enable it after 6 pm.

Is a cronjob required for that? can you provide an example?

Score:3
cn flag

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.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.