DEV Community

qjlsh
qjlsh

Posted on Fully Autonomous

Keep Your Jobs Alive with tmux

Introduction to tmux

tmux is a terminal multiplexer that allows you to create, attach to, and switch between multiple terminal sessions within a single window. This tool is particularly useful for running long-running jobs on remote servers, ensuring they continue to execute even if you temporarily lose connection. To get started with tmux, open your terminal and install it if it's not already available. On most Linux distributions, you can use the package manager to install it using the appropriate command for your distribution.

Starting a tmux Session

To start a new tmux session, simply type tmux in your terminal. By default, tmux will create a new session and assign it a unique session name. You can specify a name by adding new -s session_name to the command, for example, tmux new -s my_job. Once you start a session, you will be inside it immediately.

Detaching from a tmux Session

While working on a long-running job, you might need to step away from your terminal. To detach from a tmux session without stopping your job, press Ctrl+b followed by d. This will detach you from the session but keep it running. You can check the status of your tmux sessions by typing tmux ls, which will list all running sessions along with their names and IDs.

Reattaching to a tmux Session

If you need to rejoin a session, use the tmux attach command followed by the session name or the session ID. For example, tmux attach -t my_job will reattach you to the my_job session. If you forget the name or ID, you can use tmux attach without arguments to reattach to the last session you were using.

Running Long-Running Jobs in tmux

To run a long-running job in tmux, start by creating a new session with your job command. For instance, if you're running a Python script, you would use tmux new -s my_job 'python myscript.py'. Once the job starts, you can detach from the session as described earlier. The job will continue to run even if you disconnect from the terminal. To monitor the progress, reattach to the session using tmux attach -t my_job.

Using tmux with Remote Servers

When working with remote servers, you can use tmux to manage your sessions effectively. First, open a terminal and use ssh to connect to your remote server, then start a tmux session as you would locally. For example, ssh user@remote_host tmux new -s remote_job. This allows you to run long processes on a remote machine without worrying about your connection. If you need to interrupt the process, simply detach from the session on the remote server as usual. To reattach, use ssh user@remote_host tmux attach -t remote_job. This setup is particularly useful for tasks that require monitoring or occasional adjustments, such as database migrations or server deployments.

Advanced Session Management with tmux

tmux offers several advanced features for managing sessions. You can split your tmux window into panes to run multiple commands simultaneously. To split horizontally, use Ctrl+b followed by %, and to split vertically, use Ctrl+b followed by " (double quote). To switch between panes, use Ctrl+b followed by the arrow keys. You can also rename sessions by using tmux rename-session -t session_name:new_name. Additionally, tmux sessions can be configured using a configuration file (.tmux.conf). This customization can greatly enhance your productivity while managing complex tasks.

Logging and Monitoring in tmux

Monitoring the output of your long-running jobs in tmux can be streamlined with redirection to a log file. When starting your tmux session, you can redirect the output to a file by modifying your command. For example, tmux new -s my_job 'python myscript.py > myscript.log 2>&1' will run your script and pipe both standard output and standard error to myscript.log. You can then monitor this log file using tail -f myscript.log within tmux. This method is particularly useful for troubleshooting issues without having to reattach to the session. Furthermore, you can use tmux's capture-pane and capture-window commands to save the contents of your screens to files, providing an additional layer of logging and backup.


This article was produced by a fully automated pipeline: a language model wrote the draft and automated checks reviewed it. No human author is credited. It is published with AI disclosure under the platforms' transparency rules. If you find a factual error, please leave a comment and it will be corrected.

Top comments (0)