Technology

#How to Hibernate or Sleep Linux From the Command Line – CloudSavvy IT

“#How to Hibernate or Sleep Linux From the Command Line – CloudSavvy IT”

Shutterstock/Moriz

Hibernating and sleeping is for bears and computers alike! If consuming less electricity is your goal, then this guide will show you how to make a small script to safely hibernate, sleep or freeze your computer from the command line.

Hibernation and Sleep in Linux

Hibernating a computer will save the current contents of the computer’s memory (the RAM memory in your computer) to disk, specially to the swap space. After this, power will be dropped automatically by a call to the hardware, and when you restart the computer, the memory contents will be read back from the swap space into main memory and you can continue using the computer exactly at the point where you hibernated it.

There is one challenge when it comes to hibernating a Linux instance using the command line: you will need a swap space sufficiently large to encapsulate the entire RAM size, and potentially some extra for normal concurrent swap usage by applications. Thus, if you have 8GB of RAM memory in your system, you may want to allocate a 16-20GB swap space to ensure there is plenty. If you would like to learn how to do this, see our How to Create and Enable a Swapfile At the Linux Command Line guide.

A sleep state is very similar, though the power will not be completely taken away. Your memory, and at times your CPU, will remain under power so that when you come back to the machine, and for example press the space bar or click a mouse button (as wake events), the computer will commence operating again at the point where you put it to sleep. Such wake events can usually be configured from within the BIOS.

The BIOS of your computer can usually be accessed during the startup of your computer, provided that it was not hibernated or put to sleep but actually shutdown in full when you last used it. Often it can be accessed by pressing a key like F2 or DEL (or any other key displayed on the screen or available from your mainboard manufacturer manual) during the early startup of your system (as soon as any text is displayed on the screen after pressing the power button).

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

These levels are built into the kernel and the availability of them depend on the operating system being used. The levels are Suspend-To-Idle (aptly named s2idle or freeze), Suspend-To-Memory (mem or more correctly deep) and Suspend-To-Disk (disk). Your system may also support a Standby (standby or shallow). We will soon learn more about how to discover what levels are available on your system and how to control them!

Desktops Failing to Present Hibernate or Sleep?

Interestingly, while usually suspend-to-memory and suspend-to-disk are made available by the kernel, and supported by the system, the operating system running on top of the kernel will – with some regularity – fail to present these options in it’s GUI/Desktop.

For example, on a recent Mint 20 (a modern operating system based on Ubuntu) installation, cloned onto a modern system, the following options were available when pressing the power button in the xcfe GUI/Desktop:

No sleep, hibernate or standby offered

Log Out, Restart, Shut Down and Switch User, but no Sleep, Standby nor Hibernate! Being able to still suspend this system to memory or disk would thus be a great step forward, even if we have to call up the terminal to do so. The next section will explain how to do this, but let us first have a look at the options available on your system and what they mean.

If you execute cat /sys/power/state at your command line, you will see something similar to the following:

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

On this particular system, the three main options (freeze, mem and disk) are supported. You may also see standby or your system.

The freeze option, when set, will initiate a lightweight software-based-only sleep state for your system. The Linux user space will be frozen (hence the term) and all I/O devices will be put into a low power state. Whilst this option and state is always supported, and does limit energy uses, it is the most light form of halting your machine. If no other options are available to you, this may still save on the electricity bills, though not as much as shutting down the system.

The question for this option/state thus becomes about conscience (leaving the system in a ready-to-use and as-left-off state and saving a little versus shutting down, but having to reopen applications, and saving more). Hopefully your operating system and hardware support other states also. If they do not, consider upgrading or changing your operating system and/or updating your BIOS and other hardware devices firmware’s to their latest versions. Changing power related options in the BIOS can also help.

The mem option, when set, will initiate either a Suspend-To-Idle, which is identical to the freeze option, or do a Suspend-To-RAM, where it everything in the system is put into a low-power state except for the memory. Which option is used (Suspend-To-Idle or Suspend-To-RAM) depends on the setting of the /sys/power/mem_sleep file. You can examine the various modes of this file by simply executing cat /sys/power/mem_sleep at the terminal command line.

Generally speaking I would expect this option to be set by default to deep by default (as indicated by [code]) on all Linux operating systems, simply because when one sets the /sys/power/state option to mem, one would expect the system to write the contents to memory (i.e. Suspend-To-RAM, which is set by using deep in /sys/power/mem_sleep) rather than the Suspend-To-Idle mode as signified by s2idle.

Note also that it is thus possible to use two different ways to Suspend-To-Idle: either by writing freeze to /sys/power/state, or by writing s2idle to /sys/power/mem_sleep and subsequently writing mem to /sys/power/state.

As you can see, the terminology used is slightly confusing and intermixed due to the presence of the /sys/power/mem_sleep file. An easier way to do things, and presumably the default way on all modern Linux operating systems, would be to simply leave /sys/power/mem_sleep set to deep and write freeze to /sys/power/state to Suspend-To-Idle, and to write mem to /sys/power/state to Suspend-To-RAM.

Finally, we have the last option (provided your system does not have the standby option, which is a Power-On-Suspend option where the CPU maintains power) and that is disk. This options allows us to Suspend-To-Disk. Basically, it is the Hibernate option we described earlier, whereas Suspend-To-Idle or Suspend-To-RAM can be considered sleep modes. In this mode, all data in memory is written to disk, so that memory itself can be released of power too: a full shutdown.

In summary, the easiest way to select any of the three main power suspend options is to have (or leave) /sys/power/mem_sleep set to deep, ignore this file, and then select our suspend power modes using /sys/power/state only, and by writing freeze (Suspend-To-Idle), mem (Suspend-To-RAM), or 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 which will safely hibernate, sleep or freeze our computer. Why the mention of safety? Well, consider how writing freeze to /sys/power/state will immediately freeze the system, and restore the system to the exact same state when a key is pressed or a mouse is clicked (depending on such options being enabled via BIOS settings). Your desktop, unlocked, is still unlocked!

We thus need a script which will not just hibernate, sleep or freeze our machine but also one which will lock our desktop for us. Implementation of this will depend on the operating system, as well as the chosen Desktop/GUI.

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

We define our hibernate.sh script as follows:

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 root/super user level authentication. We simply invoice sleep 0.1 as a dummy command. The only purpose here is for subsequent sudo commands (like the last one) not to require us to enter authentication, as it is kind of hard to type a password into a terminal when your screen is locked already 😉

After this we invoke the operating system and choice of desktop/GUI/window manager specific lock command, being xflock4 for xcfe in Linux Mint 20. Next we sleep for 3 seconds using sleep 3.

Finally, we write (using echo) the word disk to /sys/power/state using tee (see man tee for more information if this is of interest, basically tee reads from standard input and writes to a file here) and sudo. This will hibernate our system to disk (and turn off the computer) provided that we have sufficient swap space as described earlier in this guide.

Changing this script to Suspend-To-RAM/Memory is now easy: simply swap the word disk to mem.

We define our sleep.sh script as follows:

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 to /sys/power/mem_sleep to be on the power-safe side, and to avoid Idle-To-Suspend. This will only work if  deep is an actual option on your system (remember to run cat /sys/power/mem_sleep to discover the memory sleep modes, and ensure deep is one of them). If deep is already selected as the current default, it will show as [deep].

Finally, we can also make a freeze script as follows:

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

Note that no additional write of s2idle to /sys/power/mem_sleep is necessary, as the freeze write to /sys/power/state automatically uses s2idle (Suspend-To-Idle). As described earlier, we could have written s2idle to /sys/power/mem_sleep and then written mem to /sys/power/state instead to achieve the same result, but this would seem to be a more confusing way to do so.

The various power states listed in scripts above, in the order presented (disk > mem > freeze) have most-to-least power savings and higher levels will put the computer into a deeper state of sleep.

Also noteworthy is that only Suspend-To-Disk (disk) provides the assurance that if somehow the power to your system drops, your saved state will not be affected and the computer can resume at the point you left off. That said, it is also the slowest as disk I/O – to the size of your memory – is necessary.

Wrapping up

Being able to sleep or hibernate a machine locally or remotely via the command line offers great flexibility, in various levels of power saving through various suspend modes. Both servers and desktops alike can be hibernated.

One can suspend a computer to idle (s2idle), to memory (mem) and to disk (disk). This guide described the various ways of doing so, as well as how this relates to the underlying hardware. Regularly operating systems, while supporting hibernation and sleep, do not always advertise or expose these options in their Desktop GUI.

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

Disclaimer: No computers where hurt in the various experiments made for this guide 🙂

If you liked the article, do not forget to share it with your friends. Follow us on Google News too, click on the star and choose us from your favorites.

For forums sites go to Forum.BuradaBiliyorum.Com

If you want to read more like this article, you can visit our Technology category.

Source

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button
Close

Please allow ads on our site

Please consider supporting us by disabling your ad blocker!