Przejdลบ do treล›ci

๐Ÿงพ Summary

This document provides a comprehensive guide to macOS launchd, the unified service management framework that replaces traditional Unix init systems, cron, and inetd. Understanding launchd is essential for managing services and automating tasks on macOS.

๐ŸŽฏ Key Concepts

Launchd serves as the single point of truth for all service management on macOS, handling: - System daemons (LaunchDaemons) - User agents (LaunchAgents) - Scheduled jobs (cron replacement) - On-demand services (inetd replacement) - Event-triggered tasks

๐Ÿ”ง Core Components

Property List (plist) Files

Services are defined using XML property lists with standardized keys: - Label: Unique service identifier - ProgramArguments: Command and arguments to execute - RunAtLoad: Start service at load time - KeepAlive: Restart if service exits

Service Types

  • Agents: Run in user context (~/Library/LaunchAgents/)
  • Daemons: Run in system context (/Library/LaunchDaemons/, /System/Library/LaunchDaemons/)

๐Ÿ“‹ Essential Commands

1
2
3
4
5
6
7
8
# Service management
launchctl load/unload plist-file     # Load/unload service
launchctl start/stop service-name    # Start/stop service
launchctl list                      # List services

# Modern commands (macOS 10.10+)
launchctl bootstrap/bootout domain path  # Bootstrap/destroy
launchctl enable/disable service    # Enable/disable

๐Ÿ› ๏ธ Common Use Cases

  1. Scheduled Tasks: Replace cron with StartCalendarInterval
  2. File Monitoring: Use WatchPaths for filesystem events
  3. Network Services: Define sockets for on-demand services
  4. Resource Management: Set limits with SoftResourceLimits

๐Ÿ” Troubleshooting Tips

  • Validate plist syntax with plutil -lint
  • Check service logs in ~/Library/Logs/
  • Monitor launchd activity with log stream
  • Use launchctl print for detailed service information