This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
This is a Linux reimplementation of macOS's caffeinate command - a lightweight bash script that prevents system sleep using systemd-inhibit and D-Bus. The project consists of:
- Main script:
caffeinate(Bash script, ~7.4KB) - Man page:
caffeinate.1(troff format) - Build system:
Makefilewith install/uninstall targets - Documentation:
README.mdwith usage examples
- Argument Parsing (
parse_args()): Handles command-line flags (-d,-i,-s,-u,-t,-w,-v) - Power Management:
check_ac_power(): Detects AC power using upower or sysfs- Battery safety: Automatically disables full sleep prevention (
-s) on battery
- Display Management:
has_gui_session(): Detects GUI environment (X11/Wayland)simulate_user_activity(): Uses gdbus/dbus-send to wake displaysinhibit_display_sleep(): Prevents display sleep via D-Bus
- System Sleep Prevention: Uses
systemd-inhibitwith modes:idle,sleep, or both - Execution Modes:
- Command mode: Runs a command with sleep prevention
- Wait mode: Waits for a PID to exit (
-w) - Timeout mode: Runs for specified duration (
-t) - Interactive mode: Runs until Ctrl+C
- Battery-aware: Full sleep prevention only works on AC power
- GUI detection: Skips display operations in headless/SSH sessions
- Auto-cleanup: Uses trap handlers to clean up child processes
- Verbose logging: Optional debug output via
-vflag
# Validate script syntax
make validate
# Install to default location (/usr/local)
make install
# Install to custom prefix
make PREFIX=/custom/path install
# Uninstall
make uninstall
# Show help
make help# Run syntax validation
bash -n caffeinate
# Test basic functionality (5 second timeout)
./caffeinate -t 5
# Test with verbose logging
./caffeinate -v -t 5
# Test display wake (if GUI available)
./caffeinate -u -t 5
# Test command execution
./caffeinate -i sleep 3- Battery vs AC behavior: Test
-sflag on both power sources - GUI detection: Test display operations in both GUI and SSH sessions
- Process cleanup: Verify child processes are terminated on Ctrl+C
- Timeout accuracy: Check if
-tdurations are respected - PID waiting: Test
-wwith various process lifetimes
- systemd-inhibit: Core mechanism for preventing sleep
- D-Bus integration: Used for display wake/sleep control (GNOME/KDE)
- Power detection: Falls back from upower to sysfs if needed
- Process management: Uses
trapfor cleanup andpkillfor child process termination - Error handling: Uses
set -euo pipefailfor strict bash error handling
caffeinate: Main bash script (entry point)caffeinate.1: Manual page (troff format)Makefile: Build/install systemREADME.md: User documentationLICENSE: MIT license
parse_args(): Command-line argument processingcheck_ac_power(): AC power detectionsimulate_user_activity(): Display wake via D-Businhibit_display_sleep(): Display sleep preventionwait_for_pid(): Process waiting logicmain(): Orchestrates all functionality