Sunday, January 13, 2013

Solving the ssh connection timeout problem

As I mentioned in my first post about setting up a Minecraft server in ec2, one of the main annoyances for my son with the original setup was that unless someone was constantly typing commands into the server, eventually the ssh connection would be closed due to inactivity and the server along with it.  I want to solve that problem now.

The specific problem that we're trying to solve reveals itself when the Minecraft server stops responding to the minecraft clients.  When we flip to the terminal window where we have the ssh connection to the EC2 instance, we see something like:

Read from remote host mctesting.no-ip.org: Connection reset by peer
Connection to mctesting.no-ip.org closed.
What has happened is that there hasn't been any activity on the ssh connection for some period of time, so it's terminated.   When that occurs, any processes on the EC2 instance that were owned by the ssh client are also killed.  In this case, that includes the java process that is our Minecraft server.

Due to the interactive nature of the Minecraft server's console, what we really want is to be able to start up the server; enter a few commands; shut down the ssh client, while keeping the server alive; reconnect to the instance and server console to enter some more commands; repeat.  This is exactly the functionality provided by a terminal multiplexer.   There a couple of different choices, but I have found that I like tmux.  It's not installed in the image that we used to create our EC2 instance, so we have to install it ourselves.  Ssh into the EC2 instance and execute the following command:

sudo yum install tmux.x86_64

After it's completed, the tmux command will be available.  Now, when we first connect to the EC2 instance, before we start the Minecraft server,  I run:

tmux new-session -s minecraft -n minecraft

This command creates a new tmux session with the name minecraft that will own the Minecraft server process.  I see this screen:



It looks just like a normal terminal, except for the green bar at the bottom - which is how I know that I'm running inside of tmux.   I execute the commands for starting up the Minecraft server and the console starts printing out information as normal.   The difference is that now I can detach from this session - even terminate the ssh connection - and the server will continue running.  To detach, I have to enter the keyboard combination of <CTRL-b> d.  If you're not familiar with that notation, this means:
  • press-and-hold the control key
  • press-and release the b key
  • release the control key
  • press-and-release the d key
This returns me to the normal terminal window with an indication that we've just [detached] from the tmux session.


We can now exit from the ssh session; wait the few minutes for the server to start; go to a different computer, ssh to the EC2 instance and reconnect to the tmux session which contains the running Minecraft server process:

tmux attach -t minecraft



No more Connection reset by peer error messages and the sudden death of your Minecraft server!

No comments:

Post a Comment