1. Introduction

We sometimes need to put our computers to sleep or wake them at specific times of the day or night when automating the running of certain tasks. An example could be downloading some files a few minutes past midnight or very early in the morning when the internet speed is sometimes faster than in the daytime.

We can also conserve a lot of energy and laptop battery life by switching PCs and laptops off during night hours.

In this tutorial, we’ll explore some methods available to suspend or wake up a Linux-based computer. Furthermore, we’ll automate the process to run every day through a cron job.

2. Using rtcwake

We can use the rtcwake command to automatically sleep, hibernate, or shut down a computer and then turn it back on at a specific time. It uses ACPI functions to control switching the computer on or off and replaces its predecessor, the nvram-wakeup command, which uses BIOS functions.

The RTC in rtcwake stands for real-time clock, which means this utility is dependent on the internal clock. We have to ensure we’ve set the system time correctly. In cases where we have a faulty CMOS battery, the command won’t work consistently.

Note that the command uses root privileges to work, so we’ll need to ensure we have a user with root privileges. Misuse of this utility may crash the system or kernel.

The rtcwake utility comes preinstalled in most Linux distros, and we can verify if it’s available on our current system:

$ which rtcwake
/usr/sbin/rtcwake

In case it’s not available, we can install it using the apt package manager:

$ sudo apt install util-linux

Alternatively, we can also use the yum package manager for RPM-based distributions:

$ sudo yum install util-linux

2.1. Using rtcwake to Suspend and Wake the System Automatically

Let’s check the basic syntax of the rtcwake command:

$ rtcwake [options] [-d device] [-m standby_mode] {-s seconds | -t time_t}

We can use rtcwake to suspend the computer to either memory or disk and wake it back up at a specific time.

For example, let’s suspend our system to RAM, then wake it up one minute later:

$ sudo rtcwake -u -s 60 -m mem
rtcwake: wakeup from "mem" using /dev/rtc0 at Wed Nov 9 15:11:05 2022

Alternatively, we can suspend the system to disk:

$ sudo rtcwake -u -s 60 -m disk
rtcwake: wakeup from "disk" using /dev/rtc0 at Wed Nov 9 15:33:35 2022

Here, we’re using the -u option to assume the hardware clock is set to UTC.

The -s option sets the wake-up time in seconds and the -m option sets the standby state.

We could also wake up our computer at a specific time of the day:

$ sudo rtcwake -m no -l -t "$(date -d 'today 16:20:00' '+%s')"

Here, we’re setting the mode to no to prevent suspending the system and only set the RTC wake-up time.

The -l option assumes the hardware clock is set to local time and the -t option sets the wakeup time to 4:20 pm today.

Additionally, we can even run a specific program after waking the computer:

$ sudo rtcwake -s 60 -m mem && code

We’re using the && operand to run the Visual Studio IDE after waking the system from RAM.

For reference, we can always use the man command to look up anything about the rtcwake command:

$ man rtcwake

2.2. Automating the rtcwake Command With cron

We can automate the rtcwake command to run every day or even specific days of a week or month. We can achieve this by creating a cron job that will keep running until we disable it.

Most Linux distros have the cron command utility installed by default.

In case it’s not available, we can install it:

$ sudo apt install cron

Let’s access the cron utility through the terminal:

$ crontab -e
Select an editor. To change later, run 'select-editor'.
1. /bin/nano <---- easiest
2. /usr/bin/vim.basic
3. /usr/bin/vim.tiny
4. /usr/bin/emacs
5. /bin/ed

Here, we can select the editor we prefer to edit the crontab file with.

Then, we add the cron job that will run the rtcwake command. Let’s add this command and then save the file:

20 16 * * * rtcwake -u -s 120 -m mem >/dev/null 2>&1

Here, we’ve set the cron job to run every day at 4:20 pm.

After running, it executes the rtcwake command that suspends the system to RAM after two minutes.

3. Using System Commands

Most modern Linux distros have adopted systemd as their system and service manager. *In systemd-managed systems, r*ebooting and shutting down the system are managed by systemctl.

Let’s explore those commands on an Ubuntu system:

$ file /sbin/{halt,poweroff,reboot,shutdown}
/sbin/halt:     symbolic link to /bin/systemctl
/sbin/poweroff: symbolic link to /bin/systemctl
/sbin/reboot:   symbolic link to /bin/systemctl
/sbin/shutdown: symbolic link to /bin/systemctl

This gives us access to these commands, and we can run them on the terminal. We require sudo privileges to run these commands.

For example, to shut down the computer, we can use the shutdown command:

$ sudo shutdown

This command shuts down the computer after a minute. However, we can specify a specific date, time, and even a message to display before shutting down.

For example, let’s shut down the system at 4:20 pm:

$ sudo shutdown -r 16:20
Shutdown scheduled for Sun 2022-11-10 16:20:00 CEST, use 'shutdown -c' to cancel.

We can also use the reboot, halt, and poweroff commands in a similar way since they’re similar to the shutdown command and have similar options.

With these commands available to us, we can use a cron job to automate running these tasks every day or on specific days of the week or the month.

First, let’s run the crontab command to edit the crontab file:

$ crontab -e

We can add this line that will immediately shut down the system at 4:20 pm*,* but only on weekdays:

$ 20 16 * * 1-5 sudo shutdown -h now

We can configure this line to work with any of the other similar commands that we’ve looked at.

Furthermore, we can even use more than one cron job to have multiple commands that suspend, wake, or shut down a system at different times.

4. Conclusion

In this article, we’ve explored two different methods to suspend, sleep, or shut down a Linux machine. Each of these methods can be run on any Linux-based distro, but in cases when the rtcwake command isn’t available, we can easily install it.

We’ve also looked at how we can automate each of these methods by using a cron job or multiple cron jobs.