Unix:Python 脚本是否一直在运行最佳实践?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30742533/
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
Unix: Have Python script constantly running best practice?
提问by Mo.
I have a Python script which process data off of a HTTP data stream and I need this script to in theory be running at all times and forever unless I kill it off manually to update it and run it again.
我有一个 Python 脚本,它处理来自 HTTP 数据流的数据,理论上我需要这个脚本一直运行并且永远运行,除非我手动关闭它以更新它并再次运行它。
I was wondering what the best practice to do this was on a Unix (Ubuntu in particular) so that even if Terminal is closed etc the script continues to run in the background unless the process or server are shut down?
我想知道在 Unix(特别是 Ubuntu)上执行此操作的最佳实践是什么,以便即使终端关闭等脚本继续在后台运行,除非进程或服务器关闭?
Thanks
谢谢
采纳答案by Jorick Spitzen
Adding your script to rc.local would work, but 'best practice' in my opinion would be to use Upstart. See this post:
将您的脚本添加到 rc.local 会起作用,但我认为“最佳实践”是使用 Upstart。看到这个帖子:
回答by Robmotron
I am not sure how "best practice" this is but you could do:
我不确定这是如何“最佳实践”,但您可以这样做:
Add the program to: /etc/rc.d/rc.local
将程序添加到:/etc/rc.d/rc.local
This will have the program run at startup.
这将使程序在启动时运行。
If you add an '&' to the end of the line it will be run in the background.
如果在行尾添加“&”,它将在后台运行。
If you dont want to run the program manually (not at startup) you could switch to another tty by pressing ctrl + alt + f1, (this opens tty1 and will work with f1 - f6) then run the command. This terminal you do not have to have open in a window so you dont have to worry about it getting closed. To return to the desktop use ctrl + alt + f7.
如果您不想手动运行程序(而不是在启动时),您可以通过按 ctrl + alt + f1 切换到另一个 tty,(这将打开 tty1 并将与 f1 - f6 一起使用)然后运行命令。您不必在窗口中打开此终端,因此您不必担心它会关闭。要返回桌面,请使用 ctrl + alt + f7。
回答by Alex Ivanov
It's an infinite loop. You can launch the script at startup and it will run forever until you kill the process itself or shut the computer down.
这是一个无限循环。您可以在启动时启动脚本,它会一直运行,直到您终止进程本身或关闭计算机。
#!/usr/bin/python
# -*- coding: utf-8 -*-
import time
while True:
print 'Hello'
time.sleep(2) # 2 second delay
回答by Ram
From your question you are implying that you are going to start the script from your terminal and not by any of Linux's startup script management methods like systemd or upstart or init.d scripts.
根据您的问题,您暗示您将从终端启动脚本,而不是通过任何 Linux 启动脚本管理方法(如 systemd 或 upstart 或 init.d 脚本)启动脚本。
If you intend to start your script from terminal, and you want it to continue to run after you close your terminal, you need to do two things
1. Make it run in the background by appending '&' after your script.
2. When you close the terminal, the shell associated to it sends HUP signal to all the processes before it dying. To ignore the HUP signal and continue to run in the background you need to start your script with 'nohup'.
tl;dr
Run your script this way:
如果您打算从终端启动脚本,并且希望它在关闭终端后继续运行,则需要做两件事
1. 通过在脚本后附加“&”使其在后台运行。
2. 当您关闭终端时,与其关联的 shell 在它死亡之前向所有进程发送 HUP 信号。要忽略 HUP 信号并继续在后台运行,您需要使用“nohup”启动脚本。
tl;dr 以
这种方式运行您的脚本:
$ nohup python mypythonscript.py &