简体   繁体   English

Unix:Python脚本是否经常运行最佳实践?

[英]Unix: Have Python script constantly running best practice?

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. 我有一个处理HTTP数据流数据的Python脚本,理论上我需要这个脚本在任何时候都是永远运行的,除非我手动将其删除以更新它并再次运行它。

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 谢谢

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. 1.在脚本后附加'&',使其在后台运行。

2. When you close the terminal, the shell associated to it sends HUP signal to all the processes before it dying. 2.关闭终端时,与之关联的shell会在死亡前向所有进程发送HUP信号。 To ignore the HUP signal and continue to run in the background you need to start your script with 'nohup'. 要忽略HUP信号并继续在后台运行,您需要使用'nohup'启动脚本。

tl;dr TL;博士
Run your script this way: 以这种方式运行脚本:

$ nohup python mypythonscript.py &

Adding your script to rc.local would work, but 'best practice' in my opinion would be to use Upstart. 将您的脚本添加到rc.local会起作用,但我认为“最佳实践”是使用Upstart。 See this post: 看这篇文章:

Daemon vs Upstart for python script Daemon vs Upstart for python脚本

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

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. 如果您不想手动运行程序(不是在启动时),您可以通过按ctrl + alt + f1切换到另一个tty,(这将打开tty1并将与f1-f6一起使用)然后运行命令。 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 + f7。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM