a computer screen with a program running on it
HacksDev

Automating Linux System Shutdown and Auto Wakeup

In scenarios where power breakdowns occur without a predictable schedule, it is crucial to have a mechanism in place to automatically shut down a Linux system to prevent data loss or hardware damage. By utilizing a few minutes of power backup provided by an uninterruptible power supply (UPS), we can implement a controlled shutdown process. In this blog post, we will provide a shell script that enables automated system shutdown and auto wakeup triggers using a configuration file. This script is particularly useful when power outages happen unexpectedly, and the system relies on UPS backup power.

Prerequisites:

  1. A Linux system running on a UPS with a few minutes of power backup.
  2. Root access or sudo privileges to modify system configuration files and execute the script.

Create the Shutdown Script

To automate the shutdown process, we will create a shell script that reads configuration values from a file and initiates the system shutdown when necessary. Open a text editor and create a new file, let’s name it auto_shutdown.sh. Replace the content with the following:

#!/bin/bash

# Wait for the server to boot
echo "Auto Sleep Service: Waiting 2 minutes for server startup..."
sleep 120

# Read the ping interval from the config file
CONFIG_FILE="/home/auto-sleep.config"
PING_IP=$(grep -oP '^\s*ping_ip\s*=\s*\K.+' "$CONFIG_FILE" | tr -d '[:space:]')
INTERVAL=$(grep -oP '^\s*sleep_interval\s*=\s*\K.+' "$CONFIG_FILE" | tr -d '[:space:]')
PING_INTERVAL=$(grep -oP '^\s*ping_interval\s*=\s*\K.+' "$CONFIG_FILE" | tr -d '[:space:]')

# Set default IP
if [ -z "$PING_IP" ]; then
    PING_IP="10.0.0.3"  # Default IP
fi

# Set a default interval if the config file doesn't specify one
if [ -z "$INTERVAL" ]; then
    INTERVAL=1800  # Default interval of 30 minutes
fi

if [ -z "$PING_INTERVAL" ]; then
    PING_INTERVAL=10  # Default interval of 10 seconds
fi

# Debug print values
echo "== auto-sleep.config =="
echo "Ping IP: $PING_IP"
echo "Ping Interval: $PING_INTERVAL"
echo "Sleep Interval: $INTERVAL"

while true; do
    echo "Auto Sleep Service: Pinging $PING_IP"
    # Ping the IP address
    ping -c 3 -W 5 $PING_IP > /dev/null
    
    # Check the ping response
    if [ $? -ne 0 ]; then
        echo "Auto Sleep Service: IP address is not responding. Going to sleep for set interval."
        sudo rtcwake -m off -s $INTERVAL

    fi
    
    sleep "$PING_INTERVAL"
doneCode language: PHP (php)

In this script, we first wait for the server to boot for a period of 2 minutes to ensure stability. Then, we read the configuration values from the specified config file (/home/orison/auto-sleep.config). The script looks for lines in the config file starting with ping_ip =sleep_interval =, and ping_interval =, extracting the values after the = sign.

Configure the Script

Open the configuration file /home/auto-sleep.config and modify the values according to your requirements. The configuration file should have the following structure:

ping_ip = 10.0.0.3
sleep_interval = 1800
ping_interval = 10

Here’s an explanation of each parameter:

  • ping_ip: The IP address to ping for checking network connectivity.
  • sleep_interval: The time interval (in seconds) to wait before checking the ping status again.
  • ping_interval: The time interval (in seconds) between each ping request.

Create a Systemd Service

To automate the execution of the script on system startup, we will create a systemd service. Create a new file called auto-sleep.service in the /etc/systemd/system/ directory and add the following content:

[Unit]
Description=Auto Sleep Service
After=network.target

[Service]
ExecStart=/path/to/auto_shutdown.sh

[Install]
WantedBy=multi-user.targetCode language: JavaScript (javascript)

Make sure to replace /path/to/auto_shutdown.sh with the actual path to the auto_shutdown.sh script.

Save the file and exit the text editor.

Enable and Start the Service

To enable and start the service, run the following commands:

sudo systemctl enable auto-sleep.service
sudo systemctl start auto-sleep.serviceCode language: CSS (css)

The service is now enabled and will start automatically on system boot.

The script utilizes the rtcwake command to initiate sleep mode based on the specified interval. By default, the script uses the rtcwake -m off -s $INTERVAL command, which puts the system into a sleep state (off) for the specified interval ($INTERVAL) before automatically waking it up.

The -m option in the rtcwake command allows you to specify different sleep modes depending on your requirements. Some commonly used modes include:

  • standby: This mode puts the system into a low-power state, allowing for a quick wakeup.
  • mem: This mode suspends the system to RAM, providing a faster wakeup but consuming more power.
  • disk: This mode suspends the system to disk (hibernation), allowing for the lowest power consumption but with a longer wakeup time.

To use a different sleep mode, modify the rtcwake command in the script accordingly. For example, to use the standby mode, replace rtcwake -m off -s $INTERVAL with rtcwake -m standby -s $INTERVAL.

Additionally, you can explore other options provided by the rtcwake command, such as setting a specific wakeup time using the -t option or specifying a relative wakeup time with the -s option.

Remember to adjust the script and configuration file according to your specific requirements and environment. Regularly test the script to ensure it functions as expected and properly handles unexpected power outages.

Automating the shutdown process in such scenarios helps protect valuable data and hardware from potential damage caused by sudden power loss. With this script and systemd service in place, you can have peace of mind, knowing that your Linux system will gracefully shut down and automatically wake up when power is restored.

Experiment with different rtcwake options and tailor the script to best suit your needs. By leveraging the power of the rtcwake command, you can create a robust and flexible system shutdown and auto wakeup mechanism.

Hi, I’m webadmin

Leave a Reply

Your email address will not be published. Required fields are marked *