Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep running a python script on AWS EC2 even if CLI session is closed

so I have a script running on (script is inside the instance) my AWS EC2 instance (Ubuntu), which should write some results of benchmarks into a txt file (also in the instance itself) every few hours...Now I have run the session with my terminal and ssh, but if I close my terminal or shut down my computer, the script obviously crashes. I have even tried: "nohup myscript.py &", but without any success.

Would really appreciate some help here

like image 740
usdq777 Avatar asked Dec 14 '17 22:12

usdq777


People also ask

How to run a Python program in AWS EC2?

Set up an AWS EC2 Instance 2. Access and use Python in the AWS Command Line Interface (CLI) 3. Run a Python program within AWS EC2 Instance Sign up for an AWS Account.

How to run command in AWS CLI?

Walkthrough: Use the AWS CLI with Run Command. Step 1: Getting started. You must either have administrator privileges on the instances you want to configure or you must have been granted the ... Step 2: Run shell scripts to view resource details. Step 3: Send simple commands using the ...

What happens if I shut down AWS EC2 server?

Please keep in mind that shutting down AWS EC2 machine also kills screen/tmux session and your script won't be restored after starting that machine again.

How do I Find my AWS EC2 instance ID?

The instance ID is available from the Amazon Elastic Compute Cloud (Amazon EC2) console. Using Run Command and the AWS-RunShellScript document, you can run any command or script on an Amazon EC2 instance as if you were logged on locally. Run the following command to view a description of the Systems Manager JSON document.


1 Answers

Like mentioned above, start a tmux window like on your Amazon ec2 instance:

tmux new -s mywindow

Once the new window starts, run your script. Once the script is running, you can close your ssh client or shut down your local computer. When you want to see the results, log in to your ec2 through ssh again, and type this:

tmux a -t mywindow

This would take you back to your original window running your script.

like image 156
RRC Avatar answered Sep 22 '22 07:09

RRC