macos 在 OS X 的后台运行 Python
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9522324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Running Python in background on OS X
提问by tkbx
Is there any way to keep my Python script (with an endless 'while' loop) running in the background on OS X? Also, for the same purpose, is there any way to have "autorun" python script on a USB drive?
有什么方法可以让我的 Python 脚本(带有无休止的“while”循环)在 OS X 的后台运行?另外,出于同样的目的,有没有办法在 USB 驱动器上“自动运行”python 脚本?
回答by mnem
If you want to have the script running as a daemon process which starts automatically, you can use launchctland a plist file.
如果您想让脚本作为自动启动的守护进程运行,您可以使用launchctl和 plist 文件。
For example, Bob has a simple python script which writes the word 'foo' to a file every second in his home directory:
例如,Bob 有一个简单的 Python 脚本,它每秒将“foo”这个词写入他的主目录中的一个文件:
#!/usr/bin/env python
import os
import time
while True:
os.system('echo " foo" >> /Users/bob/foostore.txt')
time.sleep(1)
To have it run as a daemon process, create a plist file, ~/Library/LaunchAgents/com.bobbob.osx.test.plist
, with the contents:
要将其作为守护进程运行,请创建一个 plist 文件,~/Library/LaunchAgents/com.bobbob.osx.test.plist
其内容为:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN http://www.apple.com/DTDs/PropertyList-1.0.dtd >
<plist version="1.0">
<dict>
<key>Label</key>
<string>com.bobbob.osx.test</string>
<key>Program</key>
<string>/Users/bob/pyfoo.py</string>
<key>KeepAlive</key>
<true/>
</dict>
</plist>
Then use launchctl
to load the plist from a terminal:
然后使用launchctl
从终端加载 plist:
launchctl load ~/Library/LaunchAgents/com.bobbob.osx.test.plist
This will load that script and immediately run the program in the <string>
element beneath <key>Program</key>
. You can also specify arguments for the program using a <ProgramArguments>
node with an array of <string>
elements. For more information see the launchd.plist man page
这将加载该脚本并立即在<string>
下面的元素中运行该程序<key>Program</key>
。您还可以使用具有元素<ProgramArguments>
数组的节点为程序指定参数<string>
。有关更多信息,请参阅launchd.plist 手册页
If you want to remove the script, you can use the unload command of launchctl
:
如果要删除脚本,可以使用以下的卸载命令launchctl
:
launchctl unload ~/Library/LaunchAgents/com.bobbob.osx.test.plist
The Label used in the script can be anything, but it should be unique on your system, so Apple generally uses a reversed domain name.
脚本中使用的 Label 可以是任何东西,但它在你的系统上应该是唯一的,所以苹果一般使用反向域名。
As for autorunning a script, I don't think there's any way to do that.
至于自动运行脚本,我认为没有办法做到这一点。
回答by fosstrack
See this question for ways to daemonizing python scripts on Unix like systems: Python Daemon Packaging Best Practices
有关在类 Unix 系统上守护Python 脚本的方法,请参阅此问题: Python Daemon Packaging Best Practices
Of course you can always run the script in the background as mentioned by kindall if that is all you need.
当然,如果您只需要这样,您始终可以按照 kindall 的说明在后台运行脚本。
回答by kindall
Just run your Python script in the background using the shell in the usual way:
只需以通常的方式使用 shell 在后台运行您的 Python 脚本:
python myscript.py &
As for the autorun question, this would be a huge security hole if it was something that Mac OS X did by default, so, no, of course not. But you could easily write a script that implemented something like that on purpose: periodically look for a USB drive plugged in, and do something (even run a second script from the thumb drive) when it's plugged in.
至于自动运行问题,如果这是 Mac OS X 默认所做的事情,这将是一个巨大的安全漏洞,所以,不,当然不是。但是您可以轻松编写一个有意实现类似功能的脚本:定期查找插入的 USB 驱动器,并在插入时执行某些操作(甚至从拇指驱动器运行第二个脚本)。
回答by iamprem
I tried launchctl
and couldn't make it work in El Capitan and searched around little more and found this post
我尝试过launchctl
但无法让它在 El Capitan 中工作并搜索了更多内容并找到了这篇文章
TL;DR
TL; 博士
Use shebang line #!/usr/bin/env python
or #!/path/to/python
on your script
使用 shebang 行#!/usr/bin/env python
或#!/path/to/python
在您的脚本上
chmod +x test.py
nohup /path/to/test.py &
ps ax | grep test.py
回答by Yuvraj Jaiswal
You can use nohup https://linux.die.net/man/1/nohup
你可以使用 nohup https://linux.die.net/man/1/nohup
$ nohup python <your_script.py> &
This will run your process and append the output to a file nohup.out in the same directory. & will run the script in the background as explained by other answers.
这将运行您的进程并将输出附加到同一目录中的文件 nohup.out。& 将在后台运行脚本,如其他答案所述。