Home > Process Management > tmux new-session

tmux new-session: Create a New tmux Session

The tmux new-session command creates a new tmux session, allowing users to attach to it or run it in the background. It offers various options for flexible environment management, such as naming the session, setting the initial window name, and starting the session in a specific directory.

Overview

tmux new-session is a core tmux feature that allows you to maintain terminal sessions persistently and manage tasks by separating them into multiple windows and panes. This command lets you create new workspaces and attach or detach from them as needed.

Key Features

  • Create a new tmux session
  • Ability to assign a unique name to the session
  • Start sessions in the background (detach)
  • Set initial window name and starting directory
  • Option to attach to an existing session if it already exists

Key Options

Commonly used options with the tmux new-session command.

Session Creation and Management

Generated command:

Try combining the commands.

Description:

`tmux new-session` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Various usage examples of the tmux new-session command.

Create a Basic Session

tmux new-session

In its most basic form, this creates a new, unnamed tmux session and attaches to it immediately.

Create a Named Session

tmux new-session -s my_session

Creates a new tmux session named 'my_session'. You can reconnect to this session later using its name.

Create a Session in the Background

tmux new-session -s dev_work -d

Creates a session named 'dev_work' and immediately detaches (-d) it, running it in the background. Your current terminal remains unaffected.

Start a Session in a Specific Directory

tmux new-session -s project_alpha -c /home/user/projects/alpha

Creates a session named 'project_alpha' and starts it in the '/home/user/projects/alpha' directory.

Attach if Session Exists, Otherwise Create

tmux new-session -A -s my_session

If a session named 'my_session' already exists, it attaches to it; otherwise, it creates a new one. This simplifies session management workflows.

Execute Initial Command on Session Creation

tmux new-session -s htop_monitor 'htop'

Creates a session named 'htop_monitor' and executes the 'htop' command in the first window.

Installation

tmux is not included by default in most Linux distributions, so you need to install it using the following commands.

Debian/Ubuntu

sudo apt update && sudo apt install tmux

Install tmux using the APT package manager.

CentOS/RHEL/Fedora

sudo yum install tmux
# or
sudo dnf install tmux

Install tmux using the YUM or DNF package manager.

Arch Linux

sudo pacman -S tmux

Install tmux using the Pacman package manager.

Tips & Notes

Tips and notes for using the tmux new-session command more efficiently.

Importance of Naming Sessions

When managing multiple sessions, assigning meaningful names with the -s option reduces confusion and improves work efficiency. (e.g., dev, prod, monitoring)

  • Easier session management with clear names
  • Easy reconnection using 'tmux attach -t <name>'

Utilizing Background Sessions

Using the -d option to start sessions in the background ensures that sessions persist even if you close the terminal, which is useful for long-running tasks. You can reconnect later using 'tmux attach -t <name>'.

  • Tasks persist even if server connection is lost
  • Run multiple tasks concurrently in the background

Executing Initial Commands

You can specify a command to be executed as the last argument when creating a session. This is convenient when you need to start a specific application or script immediately.

  • Example: tmux new-session -s my_app 'python app.py'
  • Run necessary services upon session startup

Automatic Session Attachment/Creation (-A)

The -A option is a convenient feature that attaches to a session if it exists, or creates a new one if it doesn't. This can be very useful in scripts or automated environments.

  • Skips the session existence check process
  • Ensures stable session access in scripts

Same category commands