DEV Community

Cover image for How to keep EC2 instance running after SSH is terminated?
Akhilesh Thite
Akhilesh Thite

Posted on

How to keep EC2 instance running after SSH is terminated?

In this blog post, I'm going to list a few commands that will help you to keep the EC2 instance running after SSH is terminated.

Author

Akhilesh Thite

Linux & Mac users

First, Log in to the server with your SSH key.

  • To keep the server running in background, use the following command.
screen -d -m
Enter fullscreen mode Exit fullscreen mode

Let's assume you have a python web application, then your command would look like this:

screen -d -m python app.py
Enter fullscreen mode Exit fullscreen mode

Or if you're using node, then your command would look like this:

screen -d -m npm start
Enter fullscreen mode Exit fullscreen mode
  • To list the available screens, use the following command.
screen -ls
Enter fullscreen mode Exit fullscreen mode
  • To restart the session, use the following command.
screen -r [session restart]
Enter fullscreen mode Exit fullscreen mode
  • To quit the session, use the following command.
screen -X -S [session you want to kill] quit
Enter fullscreen mode Exit fullscreen mode

Windows (PuTTY) users

First, SSH into your remote box. Type screen, then start the process you want.

  • Press Ctrl-A then Ctrl-D. This will detach your screen session but leave your processes running. Now, you can log out of the remote box.

  • If you want to come back later, log on again and type screen -r, this will resume your screen session and you can see the output of your process.

Have any questions or suggestions? Leave a comment down below.
Thank you!

Discussion (0)