简体   繁体   English

如何使Python应用程序在EC2实例中运行?

[英]How do I keep a Python application running in an EC2 instance?

I have a python application which I uploaded to an EC2 instance as a file named "example.py". 我有一个python应用程序,我将其作为名为“ example.py”的文件上传到EC2实例。 I want this code to be running continuously, as it has a while loop that calls a 3rd party Twitter API every minute. 我希望这段代码能够连续运行,因为它有一个while循环,每分钟都会调用第三方Twitter API。 If I run "python3 example.py", and close the terminal, the SSH session terminates. 如果运行“ python3 example.py”并关闭终端,则SSH会话终止。 Will the python application continue running in that EC2 instance? python应用程序将继续在该EC2实例中运行吗? How can I make that sure? 如何确定? Thanks. 谢谢。

Quick Solution - You run in SSH terminal: 快速解决方案 -您在SSH终端中运行:

nohup python3 example.py & 

(best if you don't wanna see what is going on later) (最好是如果您不想稍后查看发生了什么)

or 要么

Best Solution - You create a Screen in SSH terminal: 最佳解决方案 -您在SSH终端中创建一个屏幕:

screen

Then you press ctrl+a+d to detach from it. 然后按ctrl + a + d脱离它。 Then you can close SSH safely. 然后,您可以安全地关闭SSH。 If you want to see what is going on later, you can just: 如果您想稍后查看发生了什么,您可以:

screen -r

您可以使用nohup这个命令从当前会话断开连接 ,并&在后台运行:

nohup python3 example.py &

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

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