Waking Linux From Sleep Mode Using Nfc Tags

how to wake linux from sleep nfc

Linux users can put their computers to sleep or hibernate mode and have them automatically wake up at a specific time using the rtcwake command. This is included by default in most Linux systems and can be useful if you want your computer to perform tasks at a specific time without running 24/7. For example, you can put your computer to sleep at night and have it wake up to perform downloads. However, if you want to prevent your Linux OS from waking up from sleep mode by pressing buttons, you can disable wake on LAN or any other options and only allow the keyboard or mouse to wake the system.

Characteristics Values
Command rtcwake
Function To automatically sleep, hibernate, or shut down a computer and then turn it back on at a specific time
Pre-installation Comes preinstalled in most Linux distros
Installation Open a terminal window and search for "util-linux"
Clock Setting Hardware clock must be set to local time
Suspend Options Memory or disk
Wake-up Options Seconds, date, or time
Automation Can be automated with a cron job

shunsleep

Using the rtcwake command

The rtcwake command is used to put a Linux system to sleep or hibernate and wake it up at a specific time. It is included by default in most Linux systems and can be useful if you want your computer to perform tasks at a specific time without running 24/7.

The rtcwake command requires root permissions to work, so it must be run with sudo on Ubuntu and other Ubuntu-derived distributions. On Linux distributions that don't use sudo, you'll need to log in as root with the su command first.

The command uses your computer's hardware clock, which can be set in your BIOS, to determine when your computer will wake up. If you're using an older computer with a dying CMOS battery that can't keep the clock running properly, this won't work.

To wake your computer up in 60 seconds:

$ sudo rtcwake -u -s 60 -m mem

To wake your computer up in an hour:

$ sudo rtcwake -u -s 3600 -m disk

To wake your computer up at a specific time (e.g., 6:30 AM tomorrow):

$ sudo rtcwake -m no -l -t $(date +%s -d 'tomorrow 06:30')

You can also run a specific program after waking your computer up by using the "&&" operator. For example, the following command suspends your computer to RAM, wakes it up in two minutes, and then launches Firefox:

$ sudo rtcwake -m mem -s 120 && firefox

shunsleep

Setting the hardware clock

The hardware clock's basic purpose in a Linux system is to keep time when Linux is not running. You can initialise the system time to the time from the hardware clock when Linux starts up, and then never use the hardware clock again. You can also do whatever you want to the hardware clock while the system is running, and the next time Linux starts up, it will do so with the adjusted time from the hardware clock.

The hardware clock is also known as the real-time clock, the RTC, the BIOS clock, and the CMOS clock. The hardware clock is incapable of storing years outside the range 1994-1999. There is a problem in some BIOSes, almost all Award BIOSes made between 26/04/94 and 31/05/95, where they are unable to deal with years after 1999. If you have one of these machines, hwclock cannot set the year after 1999 and cannot use the value of the clock as the true time in the normal way. To compensate for this, always use the '--badyear' option. When hwclock knows it is working with a clock that cannot process years after 1999, it ignores the year part of the hardware clock value and instead tries to guess the year based on the last calibrated date in the adjtime file.

To set the hardware clock, you can use the hwclock command in the terminal, as root. This allows you to set the system clock from the hardware clock or vice versa, depending on which one is more reliable. You can also use the date command to set the system time and then use 'hwclock' to set the hardware clock. For example, 'sudo hwclock --systohc --utc' will set the hardware clock to the Coordinated Universal Time (UTC).

Note that hwclock shows the local time, even if the hardware clock is on UTC. To find out the actual time, use 'timedatectl' – "RTC time" is your hardware clock. You can also use 'ntpd -qg' and 'hwclock -w' to get the hardware clock set correctly.

shunsleep

Using cron jobs

Firstly, you need to understand the requirements and use case. For example, you may want your Linux system to sleep overnight, wake up for maintenance, go back to sleep, and then wake up early in the morning. This scenario would require four cron jobs.

The first cron job is to synchronize the OS clock with UTC (via ntp). The second job is to synchronize the hardware clock with UTC. These first two cron jobs ensure that your system clocks are accurate, which is crucial for the next steps.

The third cron job is to put the machine to sleep every evening, and you can specify the exact moment it should wake up later. This is where you need to be careful. To put the machine to sleep and wake it up at a specific time, you can use the rtcwake utility. rtcwake is a built-in kernel utility that uses a Real Time Clock (RTC) framework driver to support standard driver model wakeup flags. It is a command-line utility, so you can easily script your usage models, including cron jobs. However, rtcwake requires root or sudo permissions, and misuse could lead to data loss or system crashes, especially if your hardware does not support sleep modes well. Therefore, ensure your BIOS time is set correctly, and be cautious if your machine is a laptop, as it could overheat if left in a bag with no ventilation.

The fourth cron job would run a few minutes after the machine wakes up. It is important to leave a gap between waking up and running cron jobs to avoid any out-of-sync problems, especially if your BIOS is unstable. This fourth cron job can perform maintenance tasks such as backups and cleanup, and then put the machine back to sleep for the night.

Additionally, you can tweak your machine's sleep and wake settings using BIOS if it supports those options. You can also use the acpitool to query and set your ACPI values.

Another method to consider is using Power Management Utilities (pm-utils) to manage cron jobs. By reading the man pm-action, you can find relevant directories and programs to execute before and after suspending and hibernating.

Finally, if you want cron jobs to only run at the scheduled wake-up times and not during the active uptime of the computer, you need to disable their existing schedules. On Ubuntu, this involves disabling cron jobs in /etc/crontab and letting anacron take over the task of executing them.

shunsleep

Preventing wake-up via USB input

To prevent a Linux system from waking up via USB input, you can disable the feature via the /sys interface. Here is a step-by-step process:

  • Identify the USB Device: Use the ``lsusb` command to determine the vendor and product ID of the USB device you want to manage. For example, for a Logitech keyboard, the vendor ID is "046d" and the product ID is "c52b".
  • Locate the Device Directory: Navigate to the `/sys/bus/usb/devices` directory and find the subdirectory that matches your product. The path will look something like `/sys/bus/usb/devices/1-2.1`.
  • Disable Wake-up: To prevent the USB device from waking up the system, use the following command, replacing the subdirectory name with the one matching your product:

Bash

Echo "disabled" > /sys/bus/usb/devices/1-2.1/power/wakeup

Re-enable Wake-up (Optional): If you ever want to re-enable the wake-up feature for the USB device, simply replace "disabled" with "enabled" in the command:

Bash

Echo "enabled" > /sys/bus/usb/devices/1-2.1/power/wakeup

Automate with a Script: To automate this process, you can create a bash script that disables wake-up for all USB devices. Here is an example script:

Bash

#!/bin/bash

For f in /sys/bus/usb/devices/*/power/wakeup; do

Echo disabled > $f;

Done

Note that this script must be executed as root, on every boot, and when a new device is connected.

shunsleep

Using the /proc filesystem

The /proc filesystem is a pseudo-filesystem that provides information about the kernel and processes in a hierarchical structure. It is often used for system monitoring, configuration, and process management. Here are some details on using the /proc filesystem to wake a Linux system from sleep:

The /proc filesystem can be used to interact with and control processes, including waking them from sleep. One common location for process control is the /proc/[pid]/ directory, where [pid] is the process ID of the process you want to control. Within this directory, there are files and subdirectories that provide information about the process and allow you to modify its behaviour.

To wake a process from sleep using the /proc filesystem, you can modify the process's status by writing to specific files in the /proc/[pid]/ directory. For example, writing to the /proc/[pid]/stat file can change the process's status from sleep to running. However, the exact command may vary, and some processes may not respond to this method.

Additionally, the /proc filesystem can be used to manage power-saving features and wakeup triggers. The /proc/acpi/wakeup file contains a list of ACPI-registered wakeup sources, such as the power button, lid switch, or Wake-on-LAN functionality. By modifying this file, you can enable or disable specific wakeup sources. For example, to disable waking on opening the laptop lid, you can run: "echo none > /proc/acpi/wakeup".

It's important to note that the /proc filesystem is read-only for most files, and modifications may be temporary and require specific permissions or root access. Improper modifications can have unintended consequences, so it's important to understand the system and process before making changes.

Furthermore, the /proc filesystem can be used in conjunction with other tools and scripts to automate system suspension and wakeup. For example, you can create a script that modifies the /proc/acpi/wakeup file before suspending the system, ensuring specific wakeup sources are enabled or disabled as needed. This can be integrated with cron jobs to automate suspension and wakeup at specific times.

Frequently asked questions

You can use the rtcwake command, which is included by default with most Linux systems. This command uses your computer’s hardware clock to determine when your computer will wake up.

To install RTCWake, open a terminal window and search for “util-linux”. Then, install it the way you typically install programs.

You can use the -s option to set the wake-up time in seconds, and the -t option to set the wake-up time to a specific time. For example, to wake your computer up in 60 seconds, you would use the command "sudo rtcwake -u -s 60 -m mem".

You can check your computer's firmware settings and disable any "wake on keyboard" or "wake on USB" settings.

You can use the fg command to wake up a sleeping process, or the /proc filesystem to trigger a wakeup.

Written by
Reviewed by

Explore related products

Share this post
Print
Did this article help you?

Leave a comment