Unix-based machines are usually rebooted using the reboot command. Conversely, the poweroff command is normally executed to shut down computers running Linux.

If you’re reading this blog post and landed here after a web search, chances are you’re not able to use either or both of these commands. Executing reboot or poweroff gives an error similar to the following message:

Failed to power off system via logind: There’s already a shutdown or sleep operation in progress

This is what happened to me yesterday, and I was puzzled as it was the first time I had encountered this error. Luckily, I was able to find a way out after researching a bit about this unusual occurrence on the web. In this blog post, I share the solution that worked for me and hopefully it will work for you, too!

Talking to the Kernel

First, we’re going to switch to a console-only session. Open a TTY Terminal by pressing Alt + Ctrl + F3 simultaneously.

Note: If you are on a laptop, be sure to press the following keys instead: Alt + Ctrl + Fn + F3

Once you have access to a TTY Terminal, login by typing your username and press Enter. Then, type your password and press Enter again. Next, run the following commands:

su -
echo 1 > /proc/sys/kernel/sysrq
echo b > /proc/sysrq-trigger

Note: This solution requires you to have root access. Therefore, you should provide the root user’s password when prompted.

Your computer should restart without complaining. Congrats, you are now a Kernel Whisperer!

Magic SysRq Keys


Image courtesy of User:Stannered from Wikimedia Commons (CC BY-SA 3.0)

The solution outlined above involves writing a character to 2 different system files as the root user. Such a key combination is known as a magic SysRq key, and it allows us to send low-level commands to the kernel.

There are several combinations of magic SysRq keys, each for a specific low-level kernel command. The good thing about magic SysRq keys is that they can be invoked anytime, even during computer freezes (but not during a kernel panic, obviously). How cool is that!?

As we have seen above, the first step to invoke a magic SysRq key is to write the character 1 to the file /proc/sys/kernel/sysrq

echo 1 > /proc/sys/kernel/sysrq

This initiates the magic SysRq sequence, and the special character that’s written to the file /proc/sysrq-trigger will determine which kernel command to execute.

Kernel commandSpecial character (QWERTY)
Send the SIGTERM signal to all processes except inite
Send the SIGKILL signal to all processes except initi
Immediately reboot the system, without unmounting or syncing filesystemsb
Output current memory information to the consolem
Reset the nice level of all high-priority and real-time tasksn
Output the current registers and flags to the consolep
Sync all mounted filesystemss

For example, the following commands will display memory information:

echo 1 > /proc/sys/kernel/sysrq
echo m > /proc/sysrq-trigger

If you want to invoke SysRq kernel commands directly without first echoing 1 in the file /proc/sys/kernel/sysrq, you should add an entry in /etc/sysctl.conf as follows:

echo "kernel.sysrq = 1" >> /etc/sysctl.conf

Please refer to this page for a more complete list of magic SysRq keys.

The “Quick” and Ugly Method

If you had no luck talking to the kernel, press and hold your laptop power button for 30 seconds to initiate a cold shutdown at the firmware / BIOS level.

If that still doesn’t work, you can try removing the battery if that’s a possibility. Otherwise, I’m afraid there’s not much you can do other than waiting for the battery to drain until the laptop powers off.

Time is relative, and it should be “quick” if you’re patient enough :P

# Footnotes