Home/Guides/How to manage several long-running tmux sessions on a remote Mac mini

How to manage several long-running tmux sessions on a remote Mac mini

Updated · airankrush

What this guide covers

When you finish, each project has a session with a fixed name, you see the same state from whichever device you connect with, and one command restores the layout after a restart. You need: Homebrew and basic terminal skills.

Steps

  1. One named session per project

    After brew install tmux, create a session with tmux new -s <name>. Use the project name or its purpose as the name, for example tmux new -s api or tmux new -s agent. tmux ls lists all sessions, and tmux attach -t api reattaches to one. Do not crowd everything into one unnamed session: a remote machine runs for a long time, and a week from now you need to see at a glance what each session is doing.

  2. Four keys are enough for windows and panes

    The default prefix key is Ctrl+B. According to the official tmux Getting Started guide: after the prefix, c creates a window, % splits left and right, " splits top and bottom, and d detaches from the session. A common layout is one window running the task with a pane beside it running tail -f on the log, so both are in front of you when you come back.

  3. When the prefix key does not work in the remote desktop

    When you connect from Windows or a phone, key combinations go through the remote access client first, and some may be intercepted by the local system or the client. First check whether the client has an option for sending shortcuts to the remote side turned on. If it is still awkward, change the prefix to an easier key in ~/.tmux.conf, for example set -g prefix C-a. On a phone with no Ctrl key, use the client's own virtual keyboard or function key bar.

  4. Add two lines so scrolling and history are good enough

    Put set -g mouse on in ~/.tmux.conf and you can scroll through history with the mouse wheel and click to select panes, which in a remote desktop is far easier than paging with keys. Add set -g history-limit <lines> to raise the number of history lines kept per pane. The config is read only when the tmux server starts. After changing it, press the prefix inside a session and type :source-file ~/.tmux.conf to apply it.

  5. To take output with you, export it to a file

    Dragging to select a large block of terminal text through a remote desktop is slow and easy to get wrong. Run tmux capture-pane -p -S - > ~/pane.txt inside the session to write the entire history of the current pane to a file, then read it in an editor or sync it to your own storage. A more reliable approach is to redirect output to a log file when the task starts, and treat tmux history as a temporary view only.

  6. A restart clears every session; rebuild them with a script

    tmux sessions live in memory, so they all disappear when the machine restarts. This is not a fault. Write your layout as a script and keep it in your repository, for example:

    #!/bin/zsh
    tmux new -d -s api -c ~/code/api
    tmux new -d -s agent -c ~/code/agent
    tmux send-keys -t agent 'caffeinate -i ./run.sh' C-m

    Run it once after the machine has restarted and you have logged in to the desktop. When you need a restart, use the "Restart" option in the customer console or the Apple menu on the remote Mac. Do not shut the machine down yourself.

  7. Attaching to the same session from several devices

    Whether you connect to the remote desktop from a computer or a phone, you land on the same macOS desktop, and tmux attach joins the same session. There is no question of each device seeing something different. When two terminal windows are attached to the same session, each affects the other's display size. On the side where you only wanted a quick look, detach with the prefix and d when you are done.

FAQ

Will tasks in tmux stop when I disconnect the remote desktop?

No. Disconnecting only ends the remote access session; the remote system and tmux carry on. What does stop them is system sleep, a machine restart or the program exiting by itself. See keeping long tasks running.

Is there a way to bring the sessions back automatically after a restart?

You can hand the rebuild script above to a user-level job that starts at login. It still runs only after your account has logged in to the desktop, and automatic login needs an assessment by support first.

Is the built-in screen good enough?

It works, and the idea of detaching and reattaching is the same. tmux is more convenient when you need panes, named sessions and scripted layouts.

Pricing and availability: see the plans page · Help center (中文) · Refund policy (中文) · Telegram Contact support

Back to home · All guides