17

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

7 Answers 7

19

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
1
  • 8
    Passing --runtime to systemctl will make the mask temporary, resetting upon next boot. Commented Jun 7, 2019 at 23:35
17

The most convenient way to do it temporarily is using systemd-inhibit:

systemd-inhibit CMD [...]

With no extra options, systemd-inhibit will execute CMD while temporarily disabling idle detection (that might auto suspend/hibernate the machine), explicit suspend/hibernate (e.g. you pressed the sleep key on the keyboard by accident) and shutdown commands.

You can use it to invoke your backup script like this:

systemd-inhibit ./my-backup-task.sh
3
4

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 &
3

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
1

Here is how to do this in Xfce. (Thanks to g_p for their answer which got the ball rolling for my own.)

#!/bin/bash

# Grab current sleep timeout on battery and ac
a=$(xfconf-query -c xfce4-power-manager -p /xfce4-power-manager/inactivity-on-ac)
b=$(xfconf-query -c xfce4-power-manager -p /xfce4-power-manager/inactivity-on-battery)

# Set sleep to never on battery and ac
xfconf-query -c xfce4-power-manager -p /xfce4-power-manager/inactivity-on-ac -s 14
xfconf-query -c xfce4-power-manager -p /xfce4-power-manager/inactivity-on-battery -s 14

# Your task here
sleep 5

# Reset sleep to what it was before on battery and ac
xfconf-query -c xfce4-power-manager -p /xfce4-power-manager/inactivity-on-ac -s "$a"
xfconf-query -c xfce4-power-manager -p /xfce4-power-manager/inactivity-on-battery -s "$b"
1

Try caffeine, which can be installed with

sudo apt install caffeine
2
  • caffeine is nice but the command line program has a bug as of this writing. To make it work, launch caffeine-indicator (it gets installed along with caffeine when you do sudo apt install caffeine), click on the cup icon on the taskbar and click on Activate.
    – Mike
    Commented Mar 12, 2021 at 7:47
  • @Mike Indeed, I haven't used caffeine itself, since it requires an full-screen program. I am always using caffeine-indicator.
    – Limina102
    Commented Mar 13, 2021 at 5:54
0

2024 Update the gnome-settings no longer works as gnome added additional keys to org.gnome.settings-daemon.plugins.power. Now you just need to set sleep-inactive-ac-type and/or sleep-inactive-battery-type to either 'suspend' or 'nothing'

 $ gsettings list-keys org.gnome.settings-daemon.plugins.power
ambient-enabled
idle-brightness
idle-dim
power-button-action
power-saver-profile-on-low-battery
sleep-inactive-ac-timeout
sleep-inactive-ac-type
sleep-inactive-battery-timeout
sleep-inactive-battery-type

So the updated version is: #!/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-type)
b=$(gsettings get org.gnome.settings-daemon.plugins.power sleep-inactive-battery-type)


#Disable automatic suspend 
gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-ac-type 'nothing'
gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-battery-type 'nothing'

#Your task here
sleep 5

#Enable the automatic suspend
gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-ac-type $a
gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-battery-type $b
1
  • Which version of Ubuntu is this for? There are multiple versions that are supported right now. 20.04? 22.04? 23.10?
    – muru
    Commented Jan 16 at 4:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .