Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

keep server running on EC2 instance after ssh is terminated

Tags:

ssh

amazon-ec2

Currently, I have two servers running on an EC2 instance (MongoDB and bottlepy). Everything works when I SSHed to the instance and started those two servers. However, when I closed the SSH session (the instance is still running), I lost those two servers. Is there a way to keep the server running after logging out? I am using Bitvise Tunnelier on Windows 7. The instance I am using is Ubuntu Server 12.04.3 LTS.

like image 360
TTT Avatar asked Jan 17 '14 19:01

TTT


People also ask

How do I keep EC2 instance running after SSH is terminated?

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.

Which command is stopped after SSH logout?

Using nohup command to Keep Running SSH Sessions After that you can safely log out. With nohup command we tell the process to ignore the SIGHUP signal which is sent by ssh session on termination, thus making the command persist even after session logout.

How you can keep your webserver running even when closing the terminal window to AWS?

You can do this by typing screen in the bash prompt. Once you have fired in your command, type Ctrl-a followed by d . This will detach your screen session. However the script that you run will continue to run.


Video Answer


4 Answers

For those landing here from a google search, I would like to add tmux as another option. tmux is widely used for this purpose, and is preinstalled on new Ubuntu EC2 instances.

Managing a single session

Here is a great answer by Hamish Downer given to a similar question over at askubuntu.com:

I would use a terminal multiplexer - screen being the best known, and tmux being a more recent implementation of the idea. I use tmux, and would recommend you do to.

Basically tmux will run a terminal (or set of terminals) on a computer. If you run it on a remote server, you can disconnect from it without the terminal dying. Then when you login in again later you can reconnect, and see all the output you missed.

To start it the first time, just type

tmux

Then, when you want to disconnect, you do Ctrl+B, D (ie press Ctrl+B, then release both keys, and then press d)

When you login again, you can run

tmux attach

and you will reconnect to tmux and see all the output that happened. Note that if you accidentally lose the ssh connection (say your network goes down), tmux will still be running, though it may think it is still attached to a connection. You can tell tmux to detach from the last connection and attach to your new connection by running

tmux attach -d

In fact, you can use the -d option all the time. On servers, I have this in my >.bashrc

alias tt='tmux attach -d'

So when I login I can just type tt and reattach. You can go one step further >if you want and integrate the command into an alias for ssh. I run a mail client >inside tmux on a server, and I have a local alias:

alias maileo='ssh -t mail.example.org tmux attach -d'

This does ssh to the server and runs the command at the end - tmux attach -d The -t option ensures that a terminal is started - if a command is supplied then it is not run in a terminal by default. So now I can run maileo on a local command line and connect to the server, and the tmux session. When I disconnect from tmux, the ssh connection is also killed.

This shows how to use tmux for your specific use case, but tmux can do much more than this. This tmux tutorial will teach you a bit more, and there is plenty more out there.

Managing multiple sessions

This can be useful if you need to run several processes in the background simultaneously. To do this effectively, each session will be given a name.

Start (and connect to) a new named session:

tmux new-session -s session_name

Detach from any session as described above: Ctrl+B, D.

List all active sessions:

tmux list-sessions

Connect to a named session:

tmux attach-session -t session_name

To kill/stop a session, you have two options. One option is to enter the exit command while connected to the session you want to kill. Another option is by using the command:

tmux kill-session -t session_name
like image 162
Joakim Avatar answered Oct 08 '22 23:10

Joakim


If you don't want to run some process as a service (or via an apache module) you can (like I do for using IRC) use gnome-screen Install screen http://hostmar.co/software-small.

screen keeps running on your server even if you close the connection - and thus every process you started within will keep running too.

like image 35
guntbert Avatar answered Oct 08 '22 21:10

guntbert


It would be nice if you provided more info about your environment but assuming it's Ubuntu Linux you can start the services in the background or as daemons.

sudo service mongodb start
nohup python yourbottlepyapp.py &

(Use nohup if you want are in a ssh session and want to prevent it from closing file descriptors)

You can also run your bottle.py app using Apache mod_wsgi. (Running under the apache service) More info here: http://bottlepy.org/docs/dev/deployment.html

Hope this helps.

Addition: (your process still runs after you exit the ssh session)

Take this example time.py

 import time
 time.sleep(3600)

Then run:

 $ python3 time.py &
 [1] 3027
 $ ps -Af | grep -v grep | grep  time.py
 ubuntu    3027  2986  0 18:50 pts/3    00:00:00 python3 time.py
 $ exit

Then ssh back to the server

 $ ps -Af | grep -v grep | grep  time.py
 ubuntu    3027     1  0 18:50 ?        00:00:00 python3 time.py

Process still running (notice with no tty)

like image 3
Rico Avatar answered Oct 08 '22 21:10

Rico


You will want the started services to disconnect from the controlling terminal. I would suggest you use nohup to do that, e.g.

ssh my.server "/bin/sh -c nohup /path/to/service"

you may need to put an & in there (in the quotes) to run it in the background.

As others have commented, if you run proper init scripts to start/stop services (or ubuntu's service command), you should not see this issue.

like image 2
abligh Avatar answered Oct 08 '22 23:10

abligh