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

Plist配置中StartCalendarInterval失效,但StartInterval可正常运行

macOS Launchd plist配置问题:StartCalendarInterval无法触发Python脚本

我在配置launchd plist文件,目标是让Python脚本每日上午9点自动运行。当使用StartInterval并设置值为10(每10秒执行一次)时,脚本可以正常运行,配置代码如下:

<?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.gmtwitter.gm</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Users/marc/mambaforge/envs/main_env/bin/python</string>
        <string>/Users/marc/code/automation/twitter/twitter.py</string>
    </array>
    <key>StartInterval</key>
    <integer>10</integer>
</dict>
</plist>

但替换为StartCalendarInterval配置(设置为每日18:57运行)后,脚本从未触发。我已查阅官方文档及相关教程,仍未定位到问题,配置代码如下:

<?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.gmtwitter.gm</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Users/marc/mambaforge/envs/main_env/bin/python</string>
        <string>/Users/marc/code/automation/twitter/twitter.py</string>
    </array>
    <key>StartCalendarInterval</key>
    <dict>
        <key>Hour</key>
        <integer>18</integer>
        <key>Minute</key>
        <integer>57</integer>
    </dict>
</dict>
</plist>

排查与解决建议

  • 重新加载plist配置:修改plist后必须重新加载launchd服务,执行以下命令(确保plist放在~/Library/LaunchAgents/目录下):

    launchctl unload ~/Library/LaunchAgents/com.gmtwitter.gm.plist
    launchctl load ~/Library/LaunchAgents/com.gmtwitter.gm.plist
    
  • 确认时区与时间配置:StartCalendarInterval使用系统本地时区,检查系统时区设置是否正确。另外注意你要的是上午9点,当前配置的是18:57(下午6点57分),如果是笔误,需将Hour改为9,Minute改为0。

  • 查看launchd运行日志:通过控制台.app搜索com.gmtwitter.gm,查看是否有权限错误、路径错误或环境变量缺失的日志。也可以用命令行查看:

    log show --predicate 'process == "launchd" AND subsystem == "com.apple.launchd" AND message CONTAINS "com.gmtwitter.gm"' --info
    
  • 检查权限与环境:

    • 确保Python脚本有可执行权限:chmod +x /Users/marc/code/automation/twitter/twitter.py
    • launchd的运行环境与用户登录环境不同,脚本中若依赖特定环境变量,需在plist中添加EnvironmentVariables键配置,或者在脚本开头显式设置路径。
  • 快速测试配置:临时设置一个1-2分钟后的时间(比如当前时间10:00,设置Hour为10,Minute为02),重新加载plist后观察是否触发,快速验证配置是否生效。

内容的提问来源于stack exchange,提问作者Marc

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 17:15:35