Linux acpid Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the acpid (Advanced Configuration and Power Interface daemon) command in Linux. The acpid command is responsible for monitoring and responding to various power-related events, such as laptop lid closing, system suspend/resume, and power button presses. You will configure acpid to monitor power events and create custom event handlers to customize the system's behavior based on these events. This lab provides practical examples and step-by-step instructions to help you understand and utilize the acpid command effectively.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/VersionControlandTextEditorsGroup(["`Version Control and Text Editors`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/BasicFileOperationsGroup -.-> linux/cat("`File Concatenating`") linux/BasicFileOperationsGroup -.-> linux/chmod("`Permission Modifying`") linux/VersionControlandTextEditorsGroup -.-> linux/nano("`Simple Text Editing`") linux/SystemInformationandMonitoringGroup -.-> linux/service("`Service Managing`") subgraph Lab Skills linux/cat -.-> lab-422538{{"`Linux acpid Command with Practical Examples`"}} linux/chmod -.-> lab-422538{{"`Linux acpid Command with Practical Examples`"}} linux/nano -.-> lab-422538{{"`Linux acpid Command with Practical Examples`"}} linux/service -.-> lab-422538{{"`Linux acpid Command with Practical Examples`"}} end

Understand the acpid Command and Its Purpose

In this step, we will explore the acpid (Advanced Configuration and Power Interface daemon) command and understand its purpose in Linux systems.

The acpid command is a daemon that listens for ACPI events and executes scripts in response to those events. ACPI (Advanced Configuration and Power Interface) is a standard that defines how the operating system can interact with the hardware, particularly in power management and thermal control.

The acpid daemon is responsible for monitoring and responding to various power-related events, such as laptop lid closing, system suspend/resume, and power button presses. It allows you to customize the system's behavior based on these events.

Let's start by checking the 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

The output shows that the acpid service is running and enabled to start automatically on system boot.

Next, let's explore the default event handlers provided by the acpid service. The event handlers are defined 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 provided by default. You can customize these event handlers or create new ones to suit your specific needs.

Now, let's take a look at 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 event handler specifies that when the power button is pressed, the /etc/acpi/actions/power-button.sh script will be executed.

In the next step, we will learn how to configure acpid to monitor power events and create custom event handlers.

Configure acpid to Monitor Power Events

In this step, we will configure the acpid daemon to monitor power events and learn how to create custom event handlers.

First, let's create a new event handler to monitor the laptop lid closing event:

sudo nano /etc/acpi/events/lid-close

Add the following content to the file:

event=button/lid.*
action=/etc/acpi/actions/lid-close.sh

This event handler will execute the /etc/acpi/actions/lid-close.sh script whenever the laptop lid is closed.

Next, create the lid-close.sh script:

sudo nano /etc/acpi/actions/lid-close.sh

Add the following content to 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 suspends the system when the laptop lid is closed.

Make the script executable:

sudo chmod +x /etc/acpi/actions/lid-close.sh

Now, let's test the lid-close event handler by closing the laptop lid. You should see the system suspend after a few seconds.

To resume the system, simply open the laptop lid.

Create Custom acpid Event Handlers

In this final step, we will create a custom acpid event handler to demonstrate how you can extend the functionality of the acpid daemon.

Let's create a new event handler to monitor the battery level and perform an action when the battery drops below a certain threshold.

First, create the event handler file:

sudo nano /etc/acpi/events/low-battery

Add the following content to the file:

event=battery.*
action=/etc/acpi/actions/low-battery.sh

This event handler will execute the /etc/acpi/actions/low-battery.sh script whenever the battery level changes.

Next, create the low-battery.sh script:

sudo nano /etc/acpi/actions/low-battery.sh

Add the following content to 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 checks the current battery level using the acpi command. If the battery level is below 20%, it logs a message to the system log and sends a desktop notification.

Make the script executable:

sudo chmod +x /etc/acpi/actions/low-battery.sh

Now, let's test the low-battery event handler by discharging the battery on your laptop. When the battery level drops below 20%, you should see the notification appear on your desktop.

Summary

In this lab, we explored the acpid (Advanced Configuration and Power Interface daemon) command and its purpose in Linux systems. We learned that acpid is responsible for monitoring and responding to various power-related events, such as laptop lid closing, system suspend/resume, and power button presses. We also examined the default event handlers provided by the acpid service and understood how to customize these event handlers or create new ones to suit our specific needs.

Next, we will configure acpid to monitor power events and create custom event handlers to automate system behavior based on these events.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like