How to temporarily disable sleep and hibernate from the command line

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

I had the exact same problem last year for backups that took several hours!

You can try Keep.Awake https://launchpad.net/keep.awake It can monitor CPU load, network traffic and user activity for minimum thresholds.

I still haven't gotten around to creating a snap or deb for it. However temporarily you can download the program from Launchpad here

The current version is stable and works on all Ubuntu versions from 14.04 up to 16.04. That said I am continually improving it and will be adding new features.

It works like a proper command. Type --help to see a full listing of what can be done. The examples underneath are only a few:

./keepawake.py --help

To run interactively:

./keepawake.py

To run as a background service:

nohup ./keepawake.py -r > /dev/null 2>&1 &

To run as background service and set 15 min (900 sec) as the user activity idle time before it determines that the user is idle:

nohup ./keepawake.py -u 900 -r > /dev/null 2>&1 &

To run as background service and set minimum CPU load as 13%:

nohup ./keepawake.py -c 13 -r > /dev/null 2>&1 &

To run as background service and set minimum network traffic as 5KB (5120 bytes):

nohup ./keepawake.py -s 5120 -r > /dev/null 2>&1 &

To run all three settings above (network, CPU, User idle) in the one go:

nohup ./keepawake.py -s 5120 -c 13 -u 900 -r > /dev/null 2>&1 &

You can use gsettings in your script to disable automatic suspend in power settings and again restoring the default behavior of power setting.

Here is a simple configuration which first get the current timeout for sleep, disable it and after performing some task re-enable it.

#!/bin/bash   

#get the current timeout for automatic suspend both for on battey power and when plugged in.
a=$(gsettings get org.gnome.settings-daemon.plugins.power sleep-inactive-ac-timeout)
b=$(gsettings get org.gnome.settings-daemon.plugins.power sleep-inactive-battery-timeout)


#Disable automatic suspend 
gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-ac-timeout 0
gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-battery-timeout 0

#Your task here
sleep 5

#Enable the automatic suspend
gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-ac-timeout $a
gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-battery-timeout $b