简体   繁体   English

如何永远运行python脚本EC2?

[英]How to run python script forever EC2?

Problem is when I run a script like: 问题是当我运行如下脚本时:

python scripy.py

Then when I close the terminal the process is killed. 然后,当我关闭终端时,进程被终止。

How to run script forever under Ubuntu 16.04.3 LTS? 如何在Ubuntu 16.04.3 LTS下永久运行脚本?

I have created a service in /etc/init/publisher.conf like this: 我在/etc/init/publisher.conf创建了一个服务,如下所示:

description "Publisher"
author "alissada@gmail.com.com"

start on runlevel [2345]
stop on runlevel [!2345]

env AN_ENVIRONMENTAL_VARIABLE=i-want-to-set

respawn

exec /var/www/__init__.py

When I tried to run this service: 当我尝试运行此服务时:

service publisher start

I get an error: 我收到一个错误:

Failed to start publisher.service: Unit publisher.service not found. 无法启动Publisher.service:找不到单元Publisher.service。

Why? 为什么?

One way would be to use the 'screen' command to start detachable session (apt-get install screen if you don't have it). 一种方法是使用“屏幕”命令启动可分离会话(如果没有,则获取安装屏幕)。 'screen' creates what looks like a normal terminal, but you can detach from it (ctrl+a then d I think is the incantation). “屏幕”会创建看起来像普通终端的外观,但您可以从屏幕上拆下它(按ctrl + a然后是d,我认为是咒语)。 You can then logout, and whatever you started in the screen session will keep running. 然后,您可以注销,并且您在屏幕会话中启动的所有内容都将继续运行。

Upon your later return, you can type screen --reattach (I think, you'll have to read the man page) to return to the session you abandoned earlier. 在以后返回时,您可以键入screen --reattach(我认为,您必须阅读手册页)以返回到您先前放弃的会话。

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

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