代码之家  ›  专栏  ›  技术社区  ›  turtle

使用Python启动nchctl最小工作示例

  •  4
  • turtle  · 技术社区  · 12 年前

    我想使用launchd每分钟运行一个python脚本。我的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.turtle.script.plist</string>
        <key>ProgramArguments</key>
        <array>
            <string>/usr/bin/python</string>
            <string>/Users/turtle/Desktop/turtle.py</string>
            <string>/Users/turtle/Desktop/data/data.txt</string>
        </array>
        <key>StartInterval</key>
        <integer>60</integer>
    </dict>
    </plist>
    

    这个plist文件看起来不错,因为我得到了以下内容:

    plutil -lint com.turtle.script.plist
    com.turtle.script.plist: OK
    

    当我从命令行运行脚本时,它就可以工作了:

    /usr/bin/python /Users/turtle/Desktop/turtle.py /Users/turtle/Desktop/data/data.txt
    

    我通过以下方式加载此plist:

       launchctl load -w -F com.turtle.script.plist
    

    我也试过:

    sudo launchctl load -w -F com.turtle.script.plist
    

    我加载了这个作业,python脚本应该会把一个文件写到磁盘上。然而,从未生成过任何文件。我用以下方式检查这份工作:

    sudo launchctl list | grep com.turtle.script.plist
    

    输出为:

    - 1 com.turtle.script.plist
    

    有人能帮忙解决这个问题吗?

    3 回复  |  直到 12 年前
        1
  •  6
  •   Gordon Davisson    12 年前

    听起来脚本内部有一些环境依赖性——本质上,当你手动运行它时,它假设它运行的环境是正确的,但当launchd运行它时就不正确了。在不了解脚本的情况下,很难指出这可能是什么,但我可以建议看几件事:

    • sudo launchctl 不是更强大的版本 launchctl ,它做了一些明显不同的事情。你需要弄清楚你想要哪一个,并使用它。

      当你跑步时 启动ctl 作为普通用户(例如。 launchctl load ),它与launchd的用户实例交互,以管理Launch Agents——在用户会话中以用户身份运行的项目。

      当你跑步时 启动ctl 作为根(例如。 sudo launchctl load ),它与launchd的系统实例交互,以管理Launch Daemons——作为root在系统上下文中运行的项。

      您必须根据这个脚本的作用来决定哪一个是合适的。

    • 检查system.log(您可以使用Console实用程序查看它,或者 tail -f /var/log/system.log )并查看它是否包含任何指示脚本失败原因的内容。

    • 在launchd.plist中添加条目以记录脚本的输出,并查看其中是否包含任何错误消息或其他错误指示:

      <key>StandardOutPath</key>
      <string>/tmp/turtle.out</string>
      <key>StandardErrorPath</key>
      <string>/tmp/turtle.err</string>
      

      编辑脚本以添加调试输出可能会有所帮助,这样您就可以了解更多关于它如何工作(/不工作)的信息。

    • 脚本是否依赖于具有特定的工作目录和/或环境变量?如果是,请添加适当的 WorkingDirectory 和/或 EnvironmentVariables 项添加到.plist。

        2
  •  1
  •   epatel    12 年前

    尝试写信给 /tmp 其可以由任何用户编写。即变化 /Users/turtle/Desktop/data/data.txt /tmp/my_data.txt 如果这是您的输出文件。

        3
  •  1
  •   Thomas    10 年前

    您的.plist文件位于 ~/Library/LaunchAgents :

    <?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>local.tf.check_up</string>
        <key>Program</key>
        <string>/Users/tf/.bin/check_up.py</string>
        <key>RunAtLoad</key>
        <true/>
        <key>StandardErrorPath</key>
        <string>/tmp/local.tf.check_up.stderr</string>
        <key>StandardOutPath</key>
        <string>/tmp/local.tf.check_up.stdout</string>
        <key>StartInterval</key>
        <integer>60</integer>
        <key>WorkingDirectory</key>
        <string>/tmp/</string>
    </dict>
    </plist>
    

    你的剧本 /Users/tf/.bin/check_up.py :

    #!/opt/local/bin/python
    
    f = open('/Users/tf/Desktop/test.txt', 'a')
    f.write('hello again 4\n')
    f.close()
    

    请注意,我使用 python 来自MacPorts,它住在 /opt/local/bin/ 。如果您使用不同的python解释器,请将上面的行替换为 $ which python 返回。

    请确保您的脚本是可执行的,并且只有您有写访问权限:

    $ chmod 755 ~/.bin/check_up.py
    

    要测试脚本:运行它并检查它是否正常工作:

    $ ~/.bin/check_up.py
    

    加载LaunchAgent:

    $ launchctl load ~/Library/LaunchAgents/local.tf.check_up.plist