如何在.plist中运行Python命令?

如何在.plist中运行Python命令?,python,macos,daemon,launchd,Python,Macos,Daemon,Launchd,我想添加一个运行Python文件的LaunchDaemon。有没有简单的方法可以做到这一点?选项1 显式启动python解释器: <?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"> <

我想添加一个运行Python文件的LaunchDaemon。有没有简单的方法可以做到这一点?

选项1 显式启动python解释器:

<?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>RunAtLoad</key>
    <true/>
    <key>Label</key>
    <string>tld.yourdomain.YourService</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/bin/python</string>
        <string>/path/to/your/script.py</script>
    </array>
    <key>UserName</key>
    <string>root</string>
</dict>
</plist>
我应该说
RunAtLoad=true
不是启动作业的推荐方式。真的只有在没有其他办法的情况下才这么做。如果您想手动运行它,只需放下它并执行
launchctl start tld.yourdomain.YourService
。加载服务
launchctl load/path/To/the/plist.plist
或将其插入
/Library/LaunchAgents
/Library/LaunchDaemons
,或
~/Library/LaunchAgents

而且:
UserName=root
只有在它是启动守护进程时才可能。如果您不需要它,也可以将其删除,使其成为LaunchAgent(每个用户一个实例,而不是整个系统一个实例)。

选项1 显式启动python解释器:

<?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>RunAtLoad</key>
    <true/>
    <key>Label</key>
    <string>tld.yourdomain.YourService</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/bin/python</string>
        <string>/path/to/your/script.py</script>
    </array>
    <key>UserName</key>
    <string>root</string>
</dict>
</plist>
我应该说
RunAtLoad=true
不是启动作业的推荐方式。真的只有在没有其他办法的情况下才这么做。如果您想手动运行它,只需放下它并执行
launchctl start tld.yourdomain.YourService
。加载服务
launchctl load/path/To/the/plist.plist
或将其插入
/Library/LaunchAgents
/Library/LaunchDaemons
,或
~/Library/LaunchAgents

而且:
UserName=root
只有在它是启动守护进程时才可能。如果您不需要它,也可以将其删除,并使其成为LaunchAgent(每个用户一个实例,而不是整个系统一个实例)