How to hibernate or suspend Linux from the command line

Contents

Hibernate and sleep is for both bears and PCs!! If your goal is to consume less electricity, this guide will show you how to make a little script to hibernate, suspend or freeze your computer safely from the command line.

Hibernation and To sleep en Linux

Hibernating a computer will save the current contents of the computer's memory (the RAM memory on your computer) to disk, especially to the exchange space. After that, the power will be cut off automatically by a call to the hardware, and when i restart the computer, the contents of memory will be read from swap space to main memory and you can continue to use the computer exactly at the point where it hibernated. that.

There is a challenge when it comes to hibernating a Linux instance using the command line: you will need a swap space large enough to encapsulate the entire size of RAM and, potentially, something extra for normal concurrent swap usage by apps. So, if you have 8 GB of RAM in your system, you might want to allocate a swap space of 16 a 20 GB to make sure there is enough. If you want to learn how to do this, see our guide How to create and enable a swap file on the Linux command line.

A state of rest is very equivalent, even though the energy will not be completely removed. His memory, and sometimes your CPU, will remain under power so that when you return to the machine, and as an example press the space bar or click a mouse button (as trigger events), the computer will start operating again at the point where you put it to sleep. As usual, these trigger events can be configured from the BIOS.

As usual, your computer's BIOS can be entered throughout your computer's startup, as long as it has not been hibernated or put to sleep, but it was completely turned off the last time you used it. Often, can be entered by pressing a key like F2 or DEL (or any other key displayed on the screen or available in the motherboard manufacturer's manual) throughout the early start of your system (as soon as some text is displayed on the screen after pressing the power button).

As usual, There are three kernel-based power-saving sleep levels available in Linux, provided by the operating system (or more specifically the kernel).

These levels are built into the kernel and their availability depends on the operating system used.. The levels are Suspend to idle (aptly named s2idle O freeze), Suspend in memory (mem or more correctly deep) and Suspend to disk (disco). Your system can also support a standby mode (standby O shallow). We will soon learn more about how to find out what levels are available on your system and how to control them!!

Are desktops unable to go into hibernation or sleep??

curiously, while memory suspend and disk suspend are usually available in the kernel and supported by the system, the operating system running on top of the kernel, with some regularity, does not present these options in its GUI / Desk.

As an example, in a recent installation of Mint 20 (a modern Ubuntu-based operating system), cloned in a modern system, the following options were available by pressing the power button in the GUI / Xcfe desktop:

No suspension offered, hibernate or standby

Sign off, restart, shutdown and switch user, but not To sleep, Support ni Hibernate! Being able to still suspend this system in memory or disk would be a big step forward., even if we have to call the terminal to do it. The next section will explain how to do this., but first let's take a look at the alternatives available on your system and what they mean.

If you run cat /sys/power/state on your command line, you will see something equivalent to the following:

freeze memory and disk options available in / sys / power / state

In this particular system, the three main options (freeze, mem and disk) its compatible. You can also see standby or your system.

the freeze The option, when set, will initiate a software-only light sleep state for your system. Linux user space will freeze (hence the term) and all E devices / S will be put into a low power consumption state. Although this option and status is always supported and limits energy use, is the lightest way to stop your machine. If you have no other options available, this can still save on electricity bills, even if not as much as shutting down the system.

So, the question for this option / state becomes over consciousness (leave the system in a ready-to-use and as-left state and save a bit instead of shutting it down, but having to reopen apps and save more). Hopefully your operating system and hardware are also compatible with other states. If that is not the case, consider updating or changing your operating system and / or update the firmware of your BIOS and other hardware devices to their latest versions. Changing the alternatives associated with power in the BIOS may also help.

the mem The option, when set, iniciará un Suspend-To-Idle, which is identical to freeze option, or suspend to RAM, where everything in the system is put into a low power state except memory. The option used (Suspend-To-Idle o Suspend-To-RAM) depends on the configuration of the /sys/power/mem_sleep proceedings. You can examine the various modes of this file simply by running cat /sys/power/mem_sleep in the terminal command line.

In general terms, I would expect this option to default to deep default (as indicated [code]) on all Linux operating systems, simply because when one sets the /sys/power/state option a mem, one would expect the system to write the content to memory (In other words, Suspend-To-RAM, that configures itself using deep on /sys/power/mem_sleep) instead of Suspend-To-Idle mode as indicated s2idle.

Also note that, so, it is feasible to use two different ways to suspend to idle: either writing freeze in order to /sys/power/state, or writing s2idle in order to /sys/power/mem_sleep and later writing mem in order to /sys/power/state.

As you can see, the terminology used is a bit confusing and intermixed due to the presence of the /sys/power/mem_sleep proceedings. An easier way to do things, and presumably the default on all modern Linux operating systems, would just leave /sys/power/mem_sleep adjusted to deep and writes freeze in order to /sys/power/state a Suspend-To-Idle, and to write mem in order to /sys/power/state a Suspender a RAM.

In conclusion, we have the last option (as long as your system does not have the standby option, which is a Power-On-Suspend alternative where the CPU maintains power) and what is disk. This option enables Suspend-To-Disk. Simply, is the Hibernate option that we previously described, while Suspend-To-Idle or Suspend-To-RAM can be considered suspend modes. In this mode, all data in memory is written to disk, so memory can also free itself of energy: a complete shutdown.

In summary, the easiest way to choose any of the three main power suspension options is to have (or leave) /sys/power/mem_sleep adjusted to deep, ignore this file and then select our power sleep modes using /sys/power/state solo, and in writing freeze (Suspend to idle), mem (Suspender a RAM), O disk (Suspend-To-Disk) to this file. Let's see how to do this safely.

How to hibernate, sleep or freeze

We want to have a script that hibernates, suspend or freeze our computer safely. Why the mention of safety? Well, consider how to write freeze in order to /sys/power/state will immediately freeze the system and restore it to the exact same state when a key is pressed or a mouse is clicked (depending on whether such options are enabled through BIOS setup). His desk, unlocked, is still unlocked!

So, we need a script that doesn't just hibernate, suspend or freeze our machine, but also lock our desktop. The implementation of this will depend on the operating system, as well as the Desktop / Chosen GUI.

A quick search on your favorite search engine for [screen] lock Linux distribution GUI from command line where screen it is a word / optional search term, Linux distribution is the name of your Linux distribution, and GUI is the name of your GUI (like xe, as an example), will reveal one or more alternatives to lock your terminal from command line. For this guide, we will use Linux Mint 20 with xcfe.

We define our hibernate.sh script the next way:

sudo sleep 0.1  # To get auth
xflock4
sleep 3
echo disk | sudo tee /sys/power/state  # Requires sufficient swap space

In the first line we invoke sudo to enable superuser-level authentication / root. We simply invoice sleep 0.1 as a fictitious command. The only purpose here is that the subsequent sweat commands (as the last) do not require us to enter authentication, since it is a bit difficult to enter a password in a terminal when your screen is already locked 😉

After that, we invoke the operating system and the choice of the desktop-specific lock command / GUI / window manager, being xflock4 for xcfe on Linux Mint 20. Then, we sleep during 3 seconds using sleep 3.

In conclusion, we wrote (using echo) the word disk in order to /sys/power/state using tee (watch man tee for more information if this is of interest, just tee reads from standard input and writes to file here) and sudo. This will hibernate our system on disk (and turn off the computer) as long as we have enough swap space as previously described in this guide.

Cambiar este script a Suspend-To-RAM / Memory is now easy: just exchange the word disk in order to mem.

We define our sleep.sh script the next way:

sudo sleep 0.1  # To get auth
xflock4
sleep 3
echo deep | sudo tee /sys/power/mem_sleep
echo mem | sudo tee /sys/power/state

We took the extra step of writing deep in order to /sys/power/mem_sleep to be on the safe side of power and to avoid idle to suspend. This will only work if deep it is a real alternative in your system (remember to run cat /sys/power/mem_sleep to discover memory sleep modes and ensure deep It is one of them). And deep is already selected as the current default, will show as [deep].

In conclusion, we can also do a freeze script the next way:

sudo sleep 0.1  # To get auth
xflock4
sleep 3
echo freeze | sudo tee /sys/power/state

Note that there is no additional writing of s2idle in order to /sys/power/mem_sleep it is necessary, since the freeze write to /sys/power/state use automatically s2idle (Suspend to idle). As previously described, we could have written s2idle in order to /sys/power/mem_sleep and then written mem in order to /sys/power/state instead, to achieve the same result, but this seems to be a more confusing way to do it.

The multiple energy states listed in the previous scripts, in the order they are presented (disk > mem > freeze) feature energy savings from highest to lowest, and higher levels will put the computer into a deeper sleep state.

It is also noteworthy that only Suspend-To-Disk (disk) Provides the assurance that if your system power drops in any way, your saved state will not be affected and the computer can resume from where it left off. Having said this, it is also the slowest, since the E / S disk, for the size of your memory, it is necessary.

Ending

Being able to sleep or hibernate a machine locally or remotely via the command line offers great flexibility, at various levels of energy saving through various sleep modes. Both servers and desktops can go into hibernation.

A computer can be suspended to idle (s2idle), to memory (mem) and to the disk (disco). This guide describes the various alternatives to do so, as well as how it relates to the underlying hardware. Common operating systems, even when they support hibernation and sleep, they don't always advertise or expose these options in their desktop GUI.

Knowing how to go one step further on the command line often saves us from not being able to hibernate or sleep a machine at all., the power to hibernate or sleep that machine remotely. Enjoy!

Disclaimer: no computers were harmed in the multiple experiments performed for this guide 🙂

Subscribe to our Newsletter

We will not send you SPAM mail. We hate it as much as you.