laptop-detect
Detect if running on a laptop
TLDR
Return an exit status of 0 if the current device is likely a laptop, else returns 1
Print the type of device that the current system is detected as
Display version
SYNOPSIS
laptop-detect
PARAMETERS
None
The laptop-detect command does not accept any command-line options or arguments. Its operation is entirely based on internal system checks.
DESCRIPTION
The laptop-detect command is a simple utility designed to determine if the current Linux system is a laptop or a desktop/server. It achieves this by checking for common indicators found on portable computers.
Primarily, it inspects the presence and content of specific files and directories related to ACPI (Advanced Configuration and Power Interface) and power supply information. This includes paths like /proc/acpi/battery, /proc/acpi/ac_adapter, and entries under /sys/class/power_supply. Additionally, it may examine DMI (Desktop Management Interface) information to infer the system's form factor.
The command is most frequently used within shell scripts and automated power management frameworks (like pm-utils) to adapt system behavior. For example, a script might enable specific power-saving measures or prepare for suspend/hibernate if laptop-detect confirms the system is a laptop. Its output is solely conveyed through its exit status: it returns 0 if a laptop is detected, and 1 otherwise, making it highly suitable for conditional logic in scripts.
CAVEATS
laptop-detect relies heavily on the kernel's ability to expose ACPI and power supply information via the /proc and /sys filesystems. If this information is unavailable, incorrectly configured, or if ACPI is not properly supported, the detection might be inaccurate.
Virtual machines or specialized embedded systems, which typically lack physical battery or ACPI adapters, will almost always be reported as non-laptops. While generally reliable for standard PCs, it may occasionally misidentify obscure hardware or systems with highly customized firmware.
EXIT STATUS
The primary method of communication for laptop-detect is its exit status. A return code of 0 indicates that the system has been detected as a laptop. A return code of 1 signifies that the system is not a laptop (e.g., a desktop, server, or VM). This design makes it highly suitable for direct use in shell script conditional statements (e.g., if laptop-detect; then ... fi).
DETECTION METHODS
laptop-detect employs several methods to determine if the system is a laptop. These include:
- Checking for the existence and content of /proc/acpi/battery and /proc/acpi/ac_adapter.
- Inspecting entries under /sys/class/power_supply for power source information.
- Analyzing DMI (Desktop Management Interface) information, if available, to identify system type or chassis form factor.
HISTORY
The laptop-detect utility emerged from the need for simple, script-friendly system form-factor identification within the Linux ecosystem, particularly for power management scripts. It's often found as part of packages like pm-utils, which provided a suite of scripts for suspend, hibernate, and other power-related actions.
Its design reflects an earlier approach to power management, relying on direct filesystem checks before the widespread adoption of more comprehensive D-Bus services like UDisks or UPower. Despite its age, it remains a robust and straightforward tool for its specific purpose, used in many legacy and simpler scripting environments.