How to Create tmux Session with a Script
Last Updated :
02 Feb, 2024
Managing terminal sessions efficiently is crucial for developers and system administrators. tmux (short for terminal multiplexer) is a powerful tool that allows you to create and manage multiple terminal sessions within a single window. It is particularly useful for working on remote servers or handling long-running processes. In this article, we will explore how to create a tmux session using a script. Automating the session creation process can save time and provide consistency across different environments.
What is Tmux?
tmux, short for terminal multiplexer, is a powerful command-line tool that allows users to create, manage, and navigate multiple terminal sessions within a single window. It enhances the productivity of developers, system administrators, and anyone working extensively in the command-line environment.
Features of Tmux:
- Managing Windows: It can open several windows within the tmux session whereby each of the sessions has more than one.
- Session Management: Within a session, there can be several windows and panes.
- Pane Splitting: The terminal session could be separated into several panes, which could be vertically and horizontally split.
- Customization: Tmux sessions provide a high level of versatility and allow for a lot of configuration.
- Detaching and Reattaching: Thus, a Tmux session can be detached without terminating it from rendering running in the background ready to reattach itself.
Creating the Script
Step 1: Let's create a simple Bash script that automates the process of creating a tmux session. Open your favorite text editor and create a new file, for example, create_tmux_session.sh.
$ nano create_tmux_session.sh
Step 2: Now we have our text editor opened type up the script as follows in the editor:
#!/bin/bash
SESSION_NAME="my_session"
# Check if the session already exists
if tmux has-session -t $SESSION_NAME 2>/dev/null; then
echo "Session $SESSION_NAME already exists. Attaching to it."
tmux attach-session -t $SESSION_NAME
else
# Create a new session and name it
tmux new-session -d -s $SESSION_NAME
# Split the window horizontally
tmux split-window -h
# Send a command to the first pane
tmux send-keys -t 0 'echo "Hello from pane 1"' C-m
# Send a command to the second pane
tmux send-keys -t 1 'echo "Hello from pane 2"' C-m
# Attach to the created session
tmux attach-session -t $SESSION_NAME
fi
- tmux has-session -t $SESSION_NAME 2>/dev/null: Determines if there is a tmux session that carries the name indicated. The -t flag names the target session, while 2>/dev/null applies to discard all error messages.
- if ... then: Statements with this preface indicate the beginning of a conditional block that will be executed if the preceding command is successful.
- tmux new-session -d -s $SESSION_NAME: They spawn a new detached tmux session with a name specified via -s $SESSION_NAME. The use of the -d flag means that the session is created but it does not initiate an immediate attachment.
- tmux split-window -h: Dividing the tmux window into the two panels, that are distributed horizontally.
- tmux send-keys -t 0 'echo "Hello from pane 1"' C-m: Sends the given keystrokes (“ echo” “Hello from pane 1” ) to the first pane (@ -t 0)*. The C-m equals pressing the Enter key.
- tmux send-keys -t 1 'echo "Hello from pane 2"' C-m: Keystroke sends to the second row where it reverts a new message.
- tmux attach-session -t $SESSION_NAME: Under that new Tmux session it attaches by the structured name.

Step 3: To save the script in nano press CTRL + X and type yes to save changes on the file, hit enter. the script is now created and ready to run.
Steps to create and execute the bash script
Step 1: To make the script executable we will chmod command just type the command as follows:
chmod +x create_tmux_session.sh

Step 2: Now we are good to go and you can run the script by typing `./create_tmux_session.sh` in your terminal and the script will run and open a new tmux session.
$ ./create_tmux_session.sh
Output:
Execute the script we have previously created, by making the script executable.

Conclusion
In conclusion, tmux proves to be an invaluable tool for developers and system administrators, offering efficient terminal session management and enhanced productivity. The provided script simplifies the process of creating a tmux session, showcasing the tool's flexibility and automation capabilities. With features like window and pane organization, session management, and remote collaboration support, tmux remains a go-to solution for optimizing command-line workflows. By leveraging customization options and automation scripts, users can use tmux to their preferences, ensuring a consistent and streamlined experience across different environments.
Similar Reads
How to Create a pop up in pygame with pgu?
In this article, we will learn about how to create a pop-up in pygame with pgu in Python. PyGame The pygame library is an open-source Python module designed to assist you in creating games and other multimedia applications. Pygame, which is built on top of the highly portable SDL (Simple DirectMedia
6 min read
How to Create a Shell Script in linux
Shell is an interface of the operating system. It accepts commands from users and interprets them to the operating system. If you want to run a bunch of commands together, you can do so by creating a shell script. Shell scripts are very useful if you need to do a task routinely, like taking a backup
7 min read
How to Get Session Token in AWS?
A session token is a popular concept that is used in AWS for giving access to some user or person for a limited amount of time, in this the user gets to access the AWS resources but only for a limited amount of time only.The purpose of the session token is to have more security in the AWS system so
6 min read
How To Create An SSH key In Terraform ?
AWS SSH keys are private secret keys used for various access related things in AWS. These can be used for resources such as EC2 instances, IAM accounts. etc. Terraform can be used for easy and direct creation of SSH key in AWS. Let's see how we can create SSH Key using Terraform. Primary Components
4 min read
How to create telnet client with asyncio in Python
Telnet is a client/server application protocol that uses TCP/IP for connection. Telnet protocol enables a user to log onto and use a remote computer as though they were connected directly to it within the local network. The system that is being used by the user for the connection is the client and t
4 min read
How to create Selenium test cases
Selenium IDEÂ is an open-source tool that is widely used in conducting automated web testing and browser automation. This tool is intended mainly for Web Application testers and developers to develop, edit, and run automated test cases for Web Applications. Table of Content What are Selenium test cas
6 min read
How to Create AWS Instance Scheduler ?
Sometimes the AWS EC2 instances are created unnecessarily, causing an unwanted bill where the resources were not used and we still have to pay for them. In such scenarios, an instance scheduler comes in handy, to avoid the hassle of redundant and extra instances and to help save money. In this artic
5 min read
How to Create Full Screen Window in Tkinter?
Prerequisite: Tkinter There are two ways to create a full screen window in tkinter using standard python library for creating GUI applications. Method 1: Using attributes() function Syntax: window_name.attributes('-fullscreen',True) We will set the parameter '-fullscreen' of attributes() to True for
3 min read
How to setup a PXE boot server with NFS on CentOS?
Pre-boot eXecution Environment, or PXE, is a protocol based on BootP, DHCP, and TFTP and is often used for remote booting and operating system installation on many connected machines to the same network. A PXE server supplies the boot and installs images for PXE, which operates in a client-server co
3 min read
How to Display Images within Tkinter Frames
Tkinter is a Python module used to create Graphical User Interface (GUI) applications. Tkinter has many widgets such as Button, Label, Text, Entry, Frame, Checkbox and more that make it easy and quick to develop desktop applications. How to Insert Image inside Frame in Tkinter?To insert an image ins
1 min read