简体   繁体   English

如何在关闭本地计算机后在AWS EC2上运行流程?

[英]How to make a process run on AWS EC2 even after closing the local machine?

I have a script that will take around 10-15 hours to complete. 我有一个脚本大约需要10-15个小时才能完成。 I want to run it on EC2 instance and after say 10 hours stop the process forever. 我想在EC2实例上运行它,然后说10个小时后永远停止进程。

Approaches-- Approaches--

CRONTAB -if I make a cronjob for the process . CRONTAB如果我为这个过程做了一个cronjob。 How to ensure it runs only once in lifetime and delete after 10 hour? 如何确保它在生命周期中只运行一次并在10小时后删除?

You need to open a screen session. 您需要打开一个屏幕会话。 You can do this by typing screen in the bash prompt. 您可以通过在bash提示符下键入screen来执行此操作。 Once you have fired in your command, type Ctrl-a followed by d . 触发命令后,键入Ctrl-a然后键入d This will detach your screen session. 这将分离您的屏幕会话。 However the script that you run will continue to run. 但是,您运行的脚本将继续运行。

Now, you can disconnect from your ssh session. 现在,您可以断开与ssh会话的连接。 And once you log back in, you can resume your session by typing in screen -r . 一旦您重新登录,您可以通过键入screen -rresume会话。 This is assuming that you have only one detached session. 这假设您只有一个detached会话。 In case you have more than one, you will have to type in the pid of the session you want to attach back to. 如果您有多个,则必须输入要附加回的会话的pid。

To check the pid of the session, you can do: 要检查会话的pid ,您可以执行以下操作:

ps aux | grep screen

This will list out the currently running screen processes. 这将列出当前正在运行的屏幕进程。 A sample output is: 示例输出是:

dhanush          1327   0.0  0.0  2443312     56 s000  S+    3Sep15   0:00.99 screen -r server -p server
dhanush         79037   0.0  0.0  2432784    600 s008  S+    5:03PM   0:00.00 grep screen

Here the pid for the relevant screen process is 1327 . 这里相关屏幕过程的pid1327 The second line belongs to the grep command that searched for the keyword screen itself. 第二行属于搜索关键字screen本身的grep命令。

tmux is another alternative for the same. tmux是另一种选择。

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

相关问题 如何在 AWS EC2 上运行 Python 代码并将 csv 文件从服务器写入我的本地计算机? - How to run a Python code on AWS EC2 and write a csv file from the server to my local machine? python:如何从EC2实例在本地计算机上运行命令 - python: How to run command on local machine from an EC2 instance 如何使用本地输入和 output 在 aws ec2 上运行代码 - How to run code on aws ec2 with local input and output 在本地机器上从 ubuntu (AWS EC2) 读取文件? - reading in a file from ubuntu (AWS EC2) on local machine? 从本地机器将 Python 连接到 AWS EC2 上的 kafka - Connect Python to kafka on AWS EC2 from local machine 如何在本地机器上打开 EC2 服务器中存在的文档? - How to open a document present in EC2 server in local machine? 如何在 AWS EC2 上的 beautifulsoup 进程被杀死之前破坏它? - How to break beautifulsoup process on AWS EC2 before it gets killed? AWS EC2:如何处理具有100个并行ec2实例的队列? - AWS EC2: How to process queue with 100 parallel ec2 instances? 使用 ansible“命令”模块从本地机器到 aws ec2 的错误 scp - Error scp from local machine to aws ec2 using ansible “command” module 如何在 aws ec2 实例上运行现有的 Django 应用程序? - How to run an existing Django application on aws ec2 instance?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM