launchctl status返回2

时间:2016-01-06 22:21:11

标签: macos launchd launchctl

我正在尝试编写一个每小时运行一次Python脚本的launchd脚本。

我在~/Library/LaunchAgents/

中创建了以下文件

com.b00tahead.engage-check.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.b00tahead.engage-check</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Library/Frameworks/Python.framework/Versions/3.5/bin/python3</string>
        <string>Projects/engage_scripts/engage_check.py</string>
    </array>
    <key>StartInterval</key>
    <integer>3600</integer>
</dict>
</plist>
然后我跑了:

launchctl load ~/Library/LaunchAgents/com.b00tahead.engage-check.plist
launchctl start com.b00tahead.engage-check

当我运行launchctl list时,它会将过程显示为:

PID Status  Label
-   2       com.b00tahead.engage-check

我不确定返回代码表示的是什么。我可以通过终端python3 Projects/engage_scripts/engage_check.py

来运行脚本

Python脚本将一些数据写入桌面上的.txt文件。

这是launchctl list com.b00tahead.engage-check返回的内容:

{
    "LimitLoadToSessionType" = "Aqua";
    "Label" = "com.b00tahead.engage-check";
    "TimeOut" = 30;
    "OnDemand" = true;
    "LastExitStatus" = 512;
    "Program" = "/Library/Frameworks/Python.framework/Versions/3.5/bin/python3";
    "ProgramArguments" = (
        "/Library/Frameworks/Python.framework/Versions/3.5/bin/python3";
        "Projects/engage_scripts/engage_check.py";
    );
};

1 个答案:

答案 0 :(得分:0)

我的猜测是ProgramArguments中的参数应该有一个绝对路径(不是Projects/...之类的相对路径。)

如果出现错误,应检查系统日志,例如

tail -f /var/log/system.log
通过指定以下行可以使用

或自定义日志:

<key>StandardOutPath</key>
<string>/var/log/myjob.log</string>
<key>StandardErrorPath</key>
<string>/var/log/myjob.log</string>

请参阅:Debugging launchd Jobs section of Creating Launch Daemons and Agents page