

Disable sleep on Ubuntu
source link: https://ericmjl.github.io/blog/2020/10/18/disable-sleep-on-ubuntu/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

I have a running Linux tower at home that contains a GPU. It runs headless, i.e. without keyboard, mouse, and monitor. However, the box sometimes goes to sleep, which means I'm unable to SSH (or mosh
) into the tower on our home network, or access a perpetually running Jupyter session that I have. Short of keeping a keyboard next to it, I thought there had to be a better way to keep the tower alive.
Turns out, it's a simple command.
Based on this StackOverflow post, to disable sleeping/suspending, we execute the following command at the terminal (after logging in):
sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target
You'll get the following output:
Created symlink /etc/systemd/system/sleep.target → /dev/null. Created symlink /etc/systemd/system/suspend.target → /dev/null. Created symlink /etc/systemd/system/hibernate.target → /dev/null. Created symlink /etc/systemd/system/hybrid-sleep.target → /dev/null.
Now, let's say we wanted to re-enable sleep/suspend. In that case, we have the inverse command available:
sudo systemctl unmask sleep.target suspend.target hibernate.target hybrid-sleep.target
You'll get the following output:
Removed /etc/systemd/system/sleep.target. Removed /etc/systemd/system/suspend.target. Removed /etc/systemd/system/hibernate.target. Removed /etc/systemd/system/hybrid-sleep.target.
The mask
/unmask
combo is what does the magic. Specifically, when masking, the command creates a symbolic link in /etc/systemd/system
for each of sleep, suspend, hybernate and hybrid-sleep, and points it to /dev/null
. Pointing to /dev/null
is a great way to redirect commands to oblivion, thus disabling sleep.
I send out a monthly newsletter with tips and tools for data scientists. Come check it out at TinyLetter.
If you would like to receive deeper, in-depth content as an early subscriber, come support me on Patreon!
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK