简体   繁体   English

尝试使用launchctl / launchd运行Python脚本,但无法加载。 退出状态为2

[英]Trying to run Python script with launchctl / launchd but it won't load. Exit status is 2

I'm trying to run a simple Python script through launchctl every 5 minutes on macOS. 我正在尝试在macOS上每5分钟通过launchctl运行一个简单的Python脚本。 I've created my playlist as follows: 我已经按照以下步骤创建了播放列表:

<?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.myscript</string>
        <key>ProgramArguments</key>
        <array>
          <string>/Users/me/Documents/myscript.py</string>
        </array>
        <key>RunAtLoad</key>
        <true/>
        <key>StartInterval</key>
        <integer>300</integer>
    </dict>
  </plist>

However, the script never runs. 但是,该脚本永远不会运行。 I've loaded and unloaded it multiple times. 我已经加载和卸载了多次。 Executing the command 执行命令

/Users/me/Documents/myscript.py

works just fine, since I added '#!/usr/bin/env python' to the beginning of the Python script. 工作正常,因为我在Python脚本的开头添加了“#!/ usr / bin / env python”。 If I run launchctl -l, I see: 如果运行launchctl -l,则会看到:

    -     2   com.myscript

I'm not sure what an exit status of 2 means. 我不确定退出状态为2意味着什么。 Can anyone help? 有人可以帮忙吗?

I got this working by using cron (which previously wasn't working either) and moving the script in question to the root of my Home folder instead of the Documents folder. 我通过使用cron(以前也不起作用)将问题脚本移到我的Home文件夹的根目录而不是Documents文件夹的目录中来工作。 I think the issue is macOS Catalina's new restrictions on access to files in the Documents or Desktop folders. 我认为问题是macOS Catalina对访问“文档”或“桌面”文件夹中的文件的新限制。 Moving the script elsewhere seems to resolve it. 将脚本移到其他地方似乎可以解决它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM