You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:macOS上Launch Daemon无法每日执行脚本问题排查

排查Launch Daemon无法定时运行的问题

我有一个Launch Daemon始终无法正常工作,它本该每日凌晨3点运行一个脚本,该脚本手动执行时无需sudo权限。以下是我的配置文件和启动命令输出,求故障原因:

配置文件(com.rsync.plist)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
"http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
        <dict>
                <key>Label</key>
                <string>com.rsync</string>
                <key>Program</key>
                <string>/Users/xserve10/Documents/rsync.sh</string>
                <key>StartCalendarInterval</key>
                <dict>
                        <key>Hour</key>
                        <integer>3</integer>
                        <key>Minute</key>
                        <integer>0</integer>
                </dict>
        </dict>
</plist>

启动命令输出

Xserve-10s-Mac-mini:Documents xserve10$ ls /Library/LaunchDaemons/com.rsync.plist
/Library/LaunchDaemons/com.rsync.plist
Xserve-10s-Mac-mini:Documents xserve10$ sudo launchctl load -w /Library/LaunchDaemons/com.rsync.plist
Password:
/Library/LaunchDaemons/com.rsync.plist: service already loaded
Load failed: 37: Operation already in progress

可能的故障原因及排查步骤

  • 权限与可执行性检查:
    Launch Daemon以root身份运行,需确保脚本及父目录对root开放:
    1. 给脚本添加可执行权限:chmod +x /Users/xserve10/Documents/rsync.sh
    2. 检查/Users/xserve10/Documents/的权限,确保root有访问权限(执行ls -ld /Users/xserve10/Documents/查看)
  • 环境变量差异:
    Launch Daemon的运行环境没有用户登录后的环境变量,脚本中所有命令需使用绝对路径(比如/usr/bin/rsync而非rsync),避免因找不到命令导致执行失败。
  • 查看运行日志:
    通过系统日志排查具体报错:
    log show --predicate 'process == "launchd" AND message CONTAINS "com.rsync"' --info --last 24h
    
  • 重新加载服务:
    先卸载已加载的服务再重新加载:
    sudo launchctl unload /Library/LaunchDaemons/com.rsync.plist
    sudo launchctl load -w /Library/LaunchDaemons/com.rsync.plist
    
  • 测试root环境下的脚本运行:
    切换到root用户执行脚本,验证是否存在root环境专属问题:
    sudo /Users/xserve10/Documents/rsync.sh
    
  • 验证plist语法:
    检查配置文件是否存在语法错误:
    plutil /Library/LaunchDaemons/com.rsync.plist
    

内容的提问来源于stack exchange,提问作者nick.sullivan510

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 18:35:35