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!

Top comments (5)

Collapse
 
shaheemshanpp profile image
Shaheem PP

Later how we can see the log?

Collapse
 
shaheemshanpp profile image
Shaheem PP

I mean after making it runing in background, hhow to see that again?

Collapse
 
pablosalazzar profile image
Pablo Salazar

I was searching a way to see the logs of a specific process but I still can't get it.

Collapse
 
burgossrodrigo profile image
Rodrigo Burgos

How we know if it's running?

Collapse
 
pablosalazzar profile image
Pablo Salazar

I think you can watch if your process is running with -> screen -ls