Ubuntu – How to temporarily disable sleep and hibernate from the command line

command linehibernatepower-managementsuspend

It seems like there are many ways in which a Ubuntu's settings can be permanently manipulated to sleep when desired as long as predetermined conditions are met. However, these are more permanent solutions that don't take into account rare tasks that may want to temporarily fore go standard policy. The problem that I am running into is that I have a very long running script to do some backups and I don't want the computer to sleep or hibernate while that particular process is running which usually take 30-50 minutes. Therefore, is there a command that I could include into my backup script that would prevent the system from hibernating/sleeping while the backup script is running and is there also a command to restore the normal default power policy after the backup has finished?

something like

#!/usr/bin/bash
#disable normal powerpolicy
disable-power-policies
backup /dev/sda /dev/sdb /dev/fioa /dev/fiob
#enable power policies.
enable-power-policies

Best Answer

On Ubuntu 16.04 LTS, I successfully used the following to disable suspend:

sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target

And this to re-enable it:

sudo systemctl unmask sleep.target suspend.target hibernate.target hybrid-sleep.target
Related Question