LochStudios  /  Help Centre  /  Game Servers  /  Run a game server persistently with screen or tmux

Run a game server persistently with screen or tmux

Keep a game process running on your LochStudios KVM VPS after you close SSH. Use screen or tmux for a first test, then systemd when you want it back after a reboot.

Updated

A game process started in a normal SSH session dies when that session ends. screen and tmux keep a terminal session on the VPS so you can detach, close SSH, and come back later. Use this for a first test. For something that starts on boot and restarts after a crash, use a systemd unit instead:

Need a VPS first? See VPS. Credentials, the IPv4, and an out-of-band console sit on that server in the portal.

Before you start

  1. Sign in at the portal, open the VPS service, and keep that page open.
  2. Connect with a sudo user, or root on a fresh box.

- Connect to your VPS via SSH from macOS or Linux
- Connect to your VPS via SSH from Windows

  1. Have the game files already installed. Examples:

- Set up a Minecraft Java Edition server on Linux
- Set up a Valheim dedicated server
- Set up a Rust dedicated server
- Set up a Counter-Strike 2 dedicated server with SteamCMD

If SSH from your computer will not connect, open the console on the same VPS service. That session does not need port 22 from your network.

We document Ubuntu 24.04 LTS. Debian is close. For AlmaLinux or Rocky Linux, install with sudo dnf install -y screen or sudo dnf install -y tmux, or open a support ticket and we will match the packages.

Run the game as the dedicated user you created (minecraft or steam), not as root.

Use screen (simple, widely available)

Install screen

sudo apt update
sudo apt install -y screen

Start a named session

As the game user:

screen -S minecraft

That opens a fresh shell inside a session named minecraft. Use another name (valheim, rust, cs2) if that is the game you are running.

Start the game

Minecraft example (match the -Xmx you chose in the Minecraft article):

cd /home/minecraft/server
java -Xmx2G -Xms2G -jar server.jar nogui

SteamCMD games use the start script from that game's article, for example ~/valheim_start.sh. Leave the process in the foreground. Do not add &.

Detach (leave it running)

Press Ctrl+A, release, then press D.

The game keeps running. You should see a line like Detached from ...minecraft. You can close SSH.

Reattach later

screen -r minecraft

List sessions if you forgot the name:

screen -ls

Example:

There is a screen on:
        2345.minecraft  (Detached)
        2346.valheim    (Detached)

If screen -r says the session is already attached (for example you left it open in another window), force the old client off and attach here:

screen -d -r minecraft

Stop the game cleanly

Reattach, then type the game's own stop command in that console (stop on Minecraft). Wait for it to exit. Then type exit to close the screen session, or start the game again.

Do not kill the session while the world is still writing to disk.

End a session

If the process is already stopped and you only want to drop the session:

screen -X -S minecraft quit

Use tmux (more features)

Install tmux

sudo apt update
sudo apt install -y tmux

Start a named session

tmux new-session -s minecraft

Start the game

Same commands as in the screen section. Stay in the foreground.

Detach

Press Ctrl+B, release, then press D.

Reattach later

tmux attach -t minecraft

List sessions:

tmux list-sessions

If a stale client is still attached:

tmux attach -d -t minecraft

Stop the game cleanly

Reattach, type stop (or that game's quit command), wait for the process to exit.

End a session

tmux kill-session -t minecraft

Only do this after the game has shut down.

Command comparison

Actionscreentmux
Create sessionscreen -S nametmux new-session -s name
List sessionsscreen -lstmux list-sessions
Attachscreen -r nametmux attach -t name
DetachCtrl+A, then DCtrl+B, then D
Take over an attached sessionscreen -d -r nametmux attach -d -t name
End sessionscreen -X -S name quittmux kill-session -t name

Extra tips

  • Another window in the same tmux session. Ctrl+B, then C. Switch with Ctrl+B, then N or P.
  • Scroll back. In screen: Ctrl+A, then [. In tmux: Ctrl+B, then [. Arrow keys move. Press Q to leave copy mode.
  • A log file as well. You can still write a log next to the console:

```bash
java -Xmx2G -Xms2G -jar server.jar nogui | tee -a server.log
```

tee keeps the console you type into and appends a copy to server.log.

If something is stuck

screen -r says there is no session. The process is gone. Start a new session and launch the game again. A reboot also clears every session.

You cannot detach. From a second SSH login, try screen -d minecraft or tmux detach-client -t minecraft, then attach again.

The console looks frozen. Press Enter. If nothing changes, you may have entered copy/scroll mode (Ctrl+A [ or Ctrl+B [). Press Q or Escape.

You closed the SSH window without detaching. The session is usually still there. Run screen -ls or tmux list-sessions and attach.

A stray process is still using the port. After you thought you stopped the game:

ps aux | grep -E 'java|valheim|RustDedicated|cs2' | grep -v grep

Stop it from the game console if you can still attach. If you must, open a support ticket before you kill -9 a world that is still saving.

Still stuck? Open a support ticket. Tell us the VPS service, whether you used screen or tmux, and the session name. Do not send the password.

What to do next

A step is unclear? Open a support ticket and we will walk through the VPS with you.


Was this article helpful?

← Back to Game Servers