The Linux Rain Linux General/Gaming News, Reviews and Tutorials

Quick Tip: How to Suspend from the Command Line

By Andrew Powell, published 30/08/2017 in Tutorials


If you've ever wondered how to put your Linux machine to sleep from the command line, here's a quick tip how. Now, you may ask, why? Why, when one can just often browse to the shutdown menu or suspend icon in your favourite desktop environment and click with the mouse? Because we can! Plus, there's some of us who aren't so fond of the rodent...

Truthfully, I often use the CLI to put my Linux boxes to sleep. On one of the boxes, the desktop, I use the i3 window manager currently, while the other (my laptop) I use Xfce. Suspending from the command line isn't too surprising in i3, as well, most things are done via keyboard there. You may find doing so in Xfce a wee bit more surprising, given there's a perfectly usable sleep button that can be clicked in the Xfce logoff/shutdown menu. Oh and of course, being a laptop I could just close the lid, but there's the whole problem of the screen hinges being partially broken off, making that a little bit difficult and restricting said laptop to not being very mobile. But I digress.

I often like to leave a terminal window open, or even multiple terminal windows, and so quickly alt-tabbing to a spare terminal window and executing a sleep/suspend command is actually very fast and efficient for me. Even more so if the command is the last in my Bash history, meaning I can just quickly alt-tab to the terminal, press the "up arrow" key and hit enter... nigh nigh, laptop.

Plus, knowing how to do things like power management from the command line can open up other possibilities for scripting and scheduling with Cron, for example. CLI stuff is cool, don't let anyone tell you otherwise! Let's get to it.

systemd distros (Ubuntu, Fedora, Arch, Debian etc)

With a distribution running systemd as the init system, this is pretty easy and likely won't require any special permissions, as systemd handles all that stuff these days (amongst pretty much everything else as well). systemd's systemctl command is used here.

systemctl suspend

Simple. You can substitute "suspend" with "poweroff" or "hibernate" as well, and the systemctl command will take care of the rest.

Non-systemd distros (Void, Gentoo, Devuan etc)

With a distribution NOT running the systemd init system, chances are we'll have to use the old school pm-* utilities, included with the pm-utils power management suite. They still work pretty much the same, the main difference is you'll likely have to grant root or super user permissions to the commands. For example:

sudo pm-suspend

Enter password, and Bob's your uncle, your machine will go to sleep. Alternatively, log in to a root terminal or console and just issue pm-suspend by itself.

Bonus tip - auto sleep after one hour

This is actually one of my other favourite uses of CLI and power management utilities. Say, for example, you're going to sleep listening to music or YouTube on your machine and wish for the machine to automatically go to sleep after one hour of play (a time that you assume you'll be asleep by), you could just quickly tap into a terminal as thus (using the systemd example):

sleep 3600 && systemctl suspend

This will invoke the sleep (wait) command (not to be confused with the suspending/sleep in this article!) for 3600 seconds (one hour) and only then will it finally invoke the suspend command. Neat, huh? Of course, you can choose any length of time that you wish. This allows you to set up your machine to predictably suspend or even power off without having to be conscious or even in the same vicinity as your computer. I told you, CLI is cool!



About the author

Andrew Powell is the editor and owner of The Linux Rain who loves all things Linux, gaming and everything in between.

Tags: tutorials suspend pm-utils systemd power-management quick-tips
blog comments powered by Disqus