rtcwake is a program that is part of the util-linux package, was developed by David Brownell and improved by Bernhard Walle. This command is used to put a Linux computer to sleep and automatically wake it up at a specified time.

This can be useful for automating tasks while also conserving power. I combined this with two simple cronjobs so that my work computer will boot up and shut itself down automatically throughout the week.

To schedule tasks and kickoff scripts in Windows, Microsoft provides Task Scheduler however on Linux we use cronjobs. I am running an Asus Z170-pro motherboard and this process worked without having to tinker with any of the advanced power management settings. Below we will look at the steps that I took to get this configured.

Bash script

#!/bin/bash

#reset rtcwake
echo 0 > /sys/class/rtc/rtc0/wakealarm

#set rtcwake to wake the computer tomorrow at 7:57am local time
/usr/sbin/rtcwake -m no -l -t $(date +%s -d 'tomorrow 07:57')

#if its Friday reset rtcwake so that it doesn't boot the computer on Saturday
if [[ $(date +%u) = 5 ]] ; then

echo 0 > /sys/class/rtc/rtc0/wakealarm

fi

#if its Saturday reset rtcwake so that it doesn't boot the computer on Sunday
if [[ $(date +%u) = 6 ]] ; then

echo 0 > /sys/class/rtc/rtc0/wakealarm

fi

exit 0

How to install the script on your computer

First create a folder from where the cronjob will reference the script

sudo mkdir -p /opt/rtcwake

Create the file

sudo touch /opt/rtcwake/rtcwake.sh

Copy the script above to your clipboard and paste it into the new file you just created. (hit f3 on your keyboard to save and then Ctrl+x to exit in nano)

sudo nano /opt/rtcwake/rtcwake.sh

Make it executable

sudo chmod +x /opt/rtcwake/rtcwake.sh

In this final section we will create two cronjobs

Switch to root

sudo su

Open crontab

crontab -e

The first cronjob will run the rtcwake.sh script under the root account every day at 11:30pm local time. (Any output is sent to /dev/null)

The second cronjob will shut down the machine regardless of who is logged on. Remember to adjust the times in the first 2 columns from the left.

# m h dom mon dow command

30 23 * * * /opt/rtcwake/rtcwake.sh > /dev/null
35 23 * * * /sbin/shutdown -h now