简体   繁体   English

与 EC2 断开连接时,在 EC2 实例上运行的进程停止

[英]Process running on EC2 instance stops when get disconnected from EC2

I am running a Java CLI utility tool on an EC2 instance.我在 EC2 实例上运行 Java CLI 实用工具。 It does some process and it writes the result to the console and also to a log file.它执行一些过程并将结果写入控制台和日志文件。 I am running it with the following command:我正在使用以下命令运行它:

java -jar <jar_file_name> |& tee output_file.txt

This processes should run for a few days.这个过程应该运行几天。 But when I get disconnected (the SSH session is closed) from the EC2 instance the process stops.但是,当我从 EC2 实例断开连接(SSH session 已关闭)时,进程停止。 How can I make the process running even when I get disconnected?即使断开连接,如何使进程运行? (or turn off my local machine) (或关闭我的本地机器)

What you are looking for is the Screen command.您正在寻找的是Screen命令。 This allows you to create a virtual, detached terminal session that is retained even if your SSH connection closes.这允许您创建一个虚拟的、分离的终端 session,即使您的 SSH 连接关闭,该终端也会保留。 The screen's processes continue to execute gracefully in the background.屏幕的进程继续在后台优雅地执行。

(Screen is not pre-installed on all systems. See your distribution's documentation for installation instructions). (屏幕并非预装在所有系统上。有关安装说明,请参阅您的发行版文档)。

Create new session:新建 session:

screen -S java_session

(Run your java script here.) (在这里运行您的 java 脚本。)

To detach (go back to SSH):要分离(返回 SSH):

CTRL + A + D

To re-attach (on new SSH session):要重新附加(在新的 SSH 会话上):

screen -r

More information on Screen: Documentation有关屏幕的更多信息:文档

Try nohup along with job launch command, nohup best suited for long running jobs, irrespective of SSH session it runs.尝试 nohup 和作业启动命令,nohup 最适合长时间运行的作业,无论它运行的是 SSH session。

nohup java -jar cloud-config-server/target/*.jar > logs/cloud-config-server.log 2>&1 &

Documentation: https://linuxize.com/post/linux-nohup-command/文档: https://linuxize.com/post/linux-nohup-command/

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

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