如何在 .plist 中 运行 Python 命令?
How to run a Python command in a .plist?
我想添加一个运行 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>
选项 2
- 使您的 python 脚本可执行 (
chmod +x /path/to/your/script.py
)
- 在第一行放一个她的刘海(
#!/usr/bin/python
或 #!/usr/bin/env 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>/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
只有在它是 LaunchDaemon 时才有可能。如果你不需要它,也把它去掉,让它成为一个 LaunchAgent(每个用户一个实例 ,而不是整个系统一个实例)。
我想添加一个运行 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>
选项 2
- 使您的 python 脚本可执行 (
chmod +x /path/to/your/script.py
) - 在第一行放一个她的刘海(
#!/usr/bin/python
或#!/usr/bin/env 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>/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
只有在它是 LaunchDaemon 时才有可能。如果你不需要它,也把它去掉,让它成为一个 LaunchAgent(每个用户一个实例 ,而不是整个系统一个实例)。