使用Python的Launchctl最小工作示例

使用Python的Launchctl最小工作示例

本文介绍了使用Python的Launchctl最小工作示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用launchd每分钟运行一个python脚本.我的plist文件看起来像这样:

I'd like to run a python script every minute using launchd. My plist file looks like this:

<?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文件看起来不错,因为我得到以下信息:

This plist file looks good, as I get the following:

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

当我从命令行运行脚本时,该脚本有效:

The script works when I run it from the command line:

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

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

I load this plist via:

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

我也尝试过:

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

我加载了此作业,并且python脚本应该将文件写到磁盘上.但是,从未产生过文件.我通过以下方式检查工作:

I load this job and the python script should write out a file to disk. However no file is ever produced. I examine the job with:

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

输出为:

- 1 com.turtle.script.plist

任何人都可以帮助解决问题吗?

Can anyone help trouble-shoot the problem?

推荐答案

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

It sounds like there's some environment dependence inside the script -- essentially, it assumes something about the environment it's running in that's correct when you run it by hand, but not when launchd runs it. Without knowing anything about the script, it's hard to point at what this might be, but I can suggest a few things to look at:

  • sudo launchctl不是launchctl的更强大版本,它做了一些明显的不同.您需要找出想要的那个,然后使用它.

  • sudo launchctl isn't a more powerful version of launchctl, it does something significantly different. You need to figure out which one you want, and use it.

当您以普通用户(例如launchctl load)运行launchctl时,它将与您的launchd用户实例进行交互,以管理启动代理-在您的用户会话中以您的用户身份运行的项目.

When you run launchctl as a normal user (e.g. launchctl load), it interacts with your user instance of launchd to manage Launch Agents -- items that run in your user session, under your user identity.

当您以root用户身份(例如sudo launchctl load)运行launchctl时,它会与launchd的系统实例进行交互以管理Launch Daemons-以root用户身份在系统上下文中运行的项目.

When you run launchctl as root (e.g. sudo launchctl load), it interacts with the system instance of launchd to manage Launch Daemons -- items that run in system context, as root.

您必须根据此脚本的用途来决定哪种方法合适.

You'll have to decide which is appropriate, based on what this script does.

检查system.log(您可以使用控制台实用程序进行查看,或者单击tail -f /var/log/system.log),并查看其中是否包含任何指示脚本失败的内容.

Check system.log (you can use the Console utility to view it, or tail -f /var/log/system.log) and see if it includes anything to indicate why the script is failing.

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

Add entries to the launchd .plist to record the script's output, and see if that includes any error messages or other indications of what's going wrong:

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

编辑脚本以添加调试输出可能会有所帮助,因此您可以进一步了解它的工作方式(/不工作).

It may help to edit the script to add debugging output, so you can tell more about how it's working (/not working).

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

Does the script depend on having a particular working directory and/or environment variables? If so, add appropriate WorkingDirectory and/or EnvironmentVariables items to the .plist.

这篇关于使用Python的Launchctl最小工作示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 21:35