Introduction to acpid
This lab provides a comprehensive guide to using the acpid (Advanced Configuration and Power Interface daemon) in Linux. Master how acpid monitors and reacts to power-related events such as closing a laptop lid, system suspend/resume cycles, and power button activations. Learn to configure acpid to oversee these crucial power events and implement custom event handlers, enabling tailored system responses. This practical, step-by-step tutorial will empower you to effectively leverage the acpid command.
Understanding the acpid Command and Its Role
Here, we delve into the acpid (Advanced Configuration and Power Interface daemon) command and its fundamental role within Linux systems.
The acpid command operates as a daemon, constantly listening for ACPI events and executing predetermined scripts in response. ACPI (Advanced Configuration and Power Interface) is a critical standard defining how the operating system interacts with hardware components, especially in power management and thermal regulation.
The acpid daemon is specifically designed to monitor and react to power-related events, including laptop lid closures, system suspend/resume actions, and power button interactions. This allows for extensive customization of system behavior triggered by these events.
To begin, let's check the current status of the acpid service:
sudo systemctl status acpid
Example output:
● acpid.service - ACPI Event Daemon
Loaded: loaded (/lib/systemd/system/acpid.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2023-04-24 12:34:56 UTC; 1 day 2h ago
Main PID: 456 (acpid)
Tasks: 1 (limit: 4915)
Memory: 1.1M
CGroup: /system.slice/acpid.service
└─456 /usr/sbin/acpid -c /etc/acpi/events -s /var/run/acpid.socket
This output confirms that the acpid service is currently running and configured to automatically start upon system boot, ensuring continuous monitoring of ACPI events.
Now, let's examine the default event handlers provided by the acpid service. These handlers are located in the /etc/acpi/events/
directory:
ls -l /etc/acpi/events/
Example output:
-rw-r--r-- 1 root root 75 Apr 24 12:34 default
-rw-r--r-- 1 root root 75 Apr 24 12:34 power-button
The default
and power-button
event handlers are standard configurations. They can be customized or replaced with new handlers to meet specific systemadmin requirements.
Let's inspect the contents of the power-button
event handler:
cat /etc/acpi/events/power-button
Example output:
event=button/power.*
action=/etc/acpi/actions/power-button.sh
This handler specifies that the /etc/acpi/actions/power-button.sh
script will be executed whenever the power button is pressed, enabling a custom response to this common event.
In the following step, we will learn how to configure acpid to monitor power events and create custom event handlers, allowing for deeper control over system behavior.
Configuring acpid for Power Event Monitoring
This section demonstrates how to configure the acpid daemon to monitor power events and how to create custom event handlers to tailor system responses.
To begin, let's create a new event handler to monitor the laptop lid closing event:
sudo nano /etc/acpi/events/lid-close
Populate the file with the following content:
event=button/lid.*
action=/etc/acpi/actions/lid-close.sh
This handler will execute the /etc/acpi/actions/lid-close.sh
script whenever the laptop lid is closed, triggering a defined action.
Now, create the lid-close.sh
script:
sudo nano /etc/acpi/actions/lid-close.sh
Insert the following code into the script:
#!/bin/bash
logger "Laptop lid closed, suspending system..."
/usr/sbin/pm-suspend
This script logs a message to the system log and then initiates system suspension when the laptop lid is closed.
Grant execute permissions to the script:
sudo chmod +x /etc/acpi/actions/lid-close.sh
Test the lid-close event handler by closing the laptop lid. The system should suspend within a few seconds.
To resume the system, simply open the laptop lid, triggering the resume process.
Creating Custom acpid Event Handlers
In this final section, we will demonstrate how to create a custom acpid event handler, showcasing the extendability of the acpid daemon.
We'll create an event handler that monitors battery levels and performs an action when the battery level falls below a specified threshold.
First, create the event handler file:
sudo nano /etc/acpi/events/low-battery
Add the following to the file:
event=battery.*
action=/etc/acpi/actions/low-battery.sh
This handler will execute the /etc/acpi/actions/low-battery.sh
script whenever the battery status changes.
Next, create the low-battery.sh
script:
sudo nano /etc/acpi/actions/low-battery.sh
Insert this code into the script:
#!/bin/bash
BATTERY_LEVEL=$(acpi -b | grep -P -o '[0-9]+%' | head -n 1 | sed 's/%//')
if [ "$BATTERY_LEVEL" -lt 20 ]; then
logger "Battery level below 20%, sending notification..."
notify-send "Low Battery" "Battery level is below 20%."
fi
This script uses the acpi
command to determine the current battery level. If the level is below 20%, it logs a message and sends a desktop notification.
Make the script executable using:
sudo chmod +x /etc/acpi/actions/low-battery.sh
To test, discharge the laptop battery. When the battery reaches below 20%, a notification should appear, indicating the low battery state and demonstrating the acpid handler in action. This can be extremely useful for a systemadmin managing multiple Linux machines.
Summary
In this lab, we have explored the acpid (Advanced Configuration and Power Interface daemon) command and its significance in Linux systems. We discovered that acpid is essential for monitoring and responding to power-related events, including laptop lid closures, system suspend/resume processes, and power button presses. We also investigated the default event handlers provided by acpid and learned how to modify them or create entirely new handlers to suit specific system needs and ensure a robust, responsive system.
Through the configuration of acpid for monitoring power events and the creation of custom event handlers, we can automate system behavior based on these events, enhancing system efficiency and responsiveness, especially when managing systems as root.