Keeping things running: screen and tmux

The whole point of a shell account is that things keep running when you close your laptop. Neither screen nor tmux is doing anything clever to achieve that: they run your programs in a session that is not attached to your SSH connection, so when the connection dies, the programs do not notice.

Both are installed. Pick one and learn it properly rather than half-learning both — this guide covers each in the same order so you can compare.

screen

screen -S bots          # start a session named "bots"
screen -ls              # list your sessions
screen -r bots          # reattach
screen -d -r bots       # steal it back from an old, stuck connection

Inside a session, every command starts with Ctrl-a:

Ctrl-a d
Detach. The session keeps running. This is the one that matters.
Ctrl-a c
New window inside the session.
Ctrl-a n / p
Next / previous window.
Ctrl-a "
List windows and pick one.
Ctrl-a A
Rename the current window.
Ctrl-a [
Scrollback mode. Arrow keys or PgUp to scroll, q to leave.

A ~/.screenrc worth having:

defscrollback 10000
startup_message off
hardstatus alwayslastline "%{= kw}%-w%{= kG}%50>%n %t%{-}%+w%<"

tmux

tmux new -s bots        # start a session named "bots"
tmux ls                 # list sessions
tmux attach -t bots     # reattach
tmux attach -d -t bots  # detach any other client first

The prefix is Ctrl-b:

Ctrl-b d
Detach.
Ctrl-b c
New window.
Ctrl-b n / p
Next / previous window.
Ctrl-b w
Interactive window list.
Ctrl-b % / "
Split the window vertically / horizontally.
Ctrl-b [
Scrollback mode, q to leave.

A small ~/.tmux.conf:

set -g history-limit 10000
set -g mouse on
setw -g mode-keys vi

How this interacts with your plan limits

This is the part that surprises people, and it is worth understanding before you hit it.

A program running inside screen or tmux is a background process here, even though it has a terminal. Screen and tmux allocate their own pseudo-terminals, so "has a terminal" cannot mean "foreground" — otherwise every bot ever started in a screen session would be classified as foreground and the plan model would say nothing useful.

The rule the system actually uses: foreground means attached to a terminal belonging to one of your live login sessions. Everything else is background. So:

  • The shell you are typing into: foreground.
  • irssi running in a screen session you have detached from: background.
  • irssi running in a screen session you are currently attached to: still background — the pty belongs to screen, not to your login.
  • The screen or tmux server process itself: background, and it counts.

So one detached session running one bot costs you two background slots, not one. Run usage to see the live count.

Practical habits

  • Name your sessions. screen -S bots, not bare screen. Six weeks later, screen -ls showing 12345.pts-0.shell tells you nothing.
  • One session, several windows beats several sessions — fewer server processes against your background limit.
  • Do not nest them. Running tmux inside screen inside tmux works, technically, and then every keystroke is a puzzle.
  • Reattaching from a second machine: use screen -d -r or tmux attach -d to kick the stale connection off first.

Starting something automatically after a reboot

Reboots here are always announced in advance, never automatic — but the box does reboot occasionally for kernel updates. To bring a bot back up by itself, use cron's @reboot:

@reboot sleep 30 && /usr/bin/screen -dmS bots /home/yourname/eggdrop/eggdrop -n

-dmS means "start detached, in a named session". The sleep 30 gives the network a moment to come up first.

« All guides