Mac终端管理自启动守护进程需用launchctl配合plist文件:系统级服务存于/Library/LaunchDaemons/,须root权限,chown root:wheel并chmod 644;加载用sudo launchctl bootstrap system /path.plist,卸载用bootout;用户级代理存于~/Library/LaunchAgents/,无需sudo,用launchctl load/unload管理。

直接用终端管理 macOS 自启动守护进程,核心是 launchctl 工具配合 plist 配置文件。系统不提供“一键开关全部”的命令,但你可以按权限层级精准控制:系统级(root)、用户级(当前账户)、第三方或自定义服务。
查看当前所有活跃的守护进程和代理
运行以下命令可列出正在运行的 launchd 任务,含状态、PID 和加载路径:
- sudo launchctl list — 查看系统级(/Library/LaunchDaemons、/System/Library/LaunchDaemons)所有已加载项
- launchctl list — 查看当前用户的代理(~/Library/LaunchAgents)
- launchctl list | grep -i "com\.example" — 快速过滤特定服务(如 com.example.backup)
加载与卸载自定义守护进程(需 root 权限)
适用于你手动写的 plist 文件,比如放在 /Library/LaunchDaemons/ 下的服务:
- sudo launchctl bootstrap system /Library/LaunchDaemons/com.example.myservice.plist — 加载并启用开机启动(推荐新语法)
- sudo launchctl bootout system/com.example.myservice — 立即停止并卸载,不删除文件
- 旧语法仍可用:sudo launchctl load -w /Library/LaunchDaemons/com.example.myservice.plist(-w 表示写入启用状态)
- 对应停用:sudo launchctl unload -w /Library/LaunchDaemons/com.example.myservice.plist
管理用户级登录代理(无需 sudo)
这类服务在你登录后自动运行,配置文件通常位于 ~/Library/LaunchAgents/:
- launchctl load ~/Library/LaunchAgents/com.myapp.agent.plist — 加载并立即运行
- launchctl unload ~/Library/LaunchAgents/com.myapp.agent.plist — 停止且不再随登录启动
- 若想保留文件但禁用开机启动,去掉
-w参数再 unload 即可
检查与修复常见权限问题
plist 文件加载失败常因权限或归属错误。正确设置能避免 “Operation not permitted” 或静默失败:
- 系统级 plist(/Library/LaunchDaemons/)必须属主为 root:wheel:sudo chown root:wheel /Library/LaunchDaemons/com.example.plist
- 权限应为 644(所有者可读写,组和其他只读):sudo chmod 644 /Library/LaunchDaemons/com.example.plist
- 若脚本被调用,确保其本身有执行权限:chmod +x /usr/local/bin/myscript.sh











