简体   繁体   English

使python脚本在Amazon EC2上永久运行

[英]Make python script to run forever on Amazon EC2

I have a python script that basically runs forever and checks a webpage every second and notifies me if any value changes. 我有一个基本上永远运行的python脚本,每秒检查一个网页,如果有任何值的变化通知我。 I placed it on an AWS EC2 instance and ran it through ssh. 我将它放在AWS EC2实例上并通过ssh运行它。 The script was running fine when I checked after half an hour or so after I started it. 我启动它后半小时左右检查后脚本运行正常。

The problem is that after a few hours when I checked again, the ssh had closed. 问题是,在我再次检查几个小时后,ssh已关闭。 When I logged back in, there was no program running. 当我重新登录时,没有程序在运行。 I checked all running processes and nothing was running. 我检查了所有正在运行的进程,但没有运行。

Can anyone teach me how to make it run forever (or until I stop it) on AWS EC2 instances? 任何人都可以教我如何让它在AWS EC2实例上永远运行(或直到我停止它)? Thanks a lot. 非常感谢。


Edit: I used the Java SSH Client provided by AWS to run the script 编辑:我使用AWS提供的Java SSH客户端来运行脚本

You can run the program using the nohup command, so that even when the SSH session closes your program continues running. 您可以使用nohup命令运行该程序,这样即使SSH会话关闭,您的程序也会继续运行。

Eg: nohup python yourscriptname.py & 例如: nohup python yourscriptname.py &

For more info you can check the man page for it using man nohup . 有关更多信息,您可以使用man nohup查看手册页。

You can use Linux screen .Linux screen tool can not only save you from disconnection disasters, but it also can increase your productivity by using multiple windows within one SSH session. 您可以使用Linux screen .Linux屏幕工具不仅可以避免断线灾难,还可以通过在一个SSH会话中使用多个窗口来提高您的工作效率。 To install: 安装:

sudo apt-get install screen

Start a new session: 开始一个新的会话:

screen -S <screen_name>

Run your process as you run it in the screen session. 在屏幕会话中运行您的过程。 If you want to back to your main terminal press key shortcut ctrl+a+d . 如果要返回主终端,请按快捷键ctrl+a+d And also view the screen by typing, 并通过键入来查看屏幕,

screen -r <screen_name>

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

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