1. Introduction

In Linux, the ability to hook onto another terminal’s output is very useful in certain scenarios. Whether we’re monitoring long-running processes, debugging issues, or working remotely, accessing terminal output from a different session can significantly enhance our efficiency. This capability allows us to keep track of ongoing tasks, troubleshoot problems in real-time, and collaborate seamlessly with team members across different locations.

In this tutorial, we’ll explore various methods to achieve this, which will help us become more proficient in managing and monitoring our Linux environments.

2. Using screen

screen is a powerful terminal multiplexer that lets us create and manage multiple terminal sessions from a single window. It’s especially useful for monitoring long-running processes and for remote work, as it enables us to detach from a session and reattach to it later, even from a different terminal.

To install screen on a Debian-based Linux system, we can use the apt-get install command:

$ sudo apt-get install screen

To start a new screen session, we use:

$ screen

Within this new session, we can run any command. For example, let’s run a simple loop that counts from 1 to 100:

$ for i in {1..100}; do echo $i; sleep 1; done

We can detach from this session and leave it running in the background by pressing Ctrl-a followed by d.

To reattach to the session from another terminal, we use:

$ screen -r

Using this set of commands we can start a screen session, run a task, and then detach and reattach to monitor its progress. Detaching allows our processes to continue running even if we close the terminal, and reattaching ensures we can always get back to our tasks. This makes screen an essential tool for terminal management and remote work.

3. Using tmux

tmux is a popular terminal multiplexer that allows us to create, manage, and switch between multiple terminal sessions within a single window. It’s particularly useful for multitasking and remote work, providing advanced features like window splitting and session persistence.

To install tmux, we use the following command:

$ sudo apt-get install tmux

To start a new tmux session, we use:

$ tmux

Within this new session, we can run any command. For example, let’s run the same simple loop that counts from 1 to 100:

$ for i in {1..100}; do echo $i; sleep 1; done

We can detach from this session and leave it running in the background by pressing Ctrl-b followed by d.

To reattach to the session from another terminal, we use:

$ tmux attach-session

These commands show how we can start a tmux session, run a task, and then detach and reattach to monitor its progress. Detaching allows our processes to continue running even if we close the terminal, and reattaching ensures we can always get back to our tasks, making tmux an essential tool for terminal management and remote work.

4. Using script

The script command is a simple yet powerful tool for recording terminal sessions. It’s useful for capturing command output, making it ideal for debugging, documentation, and monitoring processes. By recording everything that happens in the terminal, we can review and analyze the output later.

To install script, we use the following command:

$ sudo apt-get install bsdutils

To start a new recording session, we use:

$ script

This starts recording everything in the terminal session. For example, let’s again run the simple loop that counts from 1 to 100:

$ for i in {1..100}; do echo $i; sleep 1; done

We can interrupt the loop using Crtl+c and then stop the recording by typing exit.

The output is saved in a file named typescript by default. We can view this file using any text editor or command like cat:

$ cat typescript

This example shows how we can start a script session, run a task, and then stop the recording to review the output. Starting script initiates a recording session, capturing all terminal activities. Running our loop task generates output that gets recorded. Finally, exiting the session stops the recording and saves the output to a file, which we can review later. This makes script a valuable tool for capturing and analyzing terminal activities.

The script command also supports more advanced usage such as specifying a different filename for the output, appending to an existing file, and real-time monitoring of the output file. For example, to save the output to a custom file, we can use:

$ script my_output.txt

To append to an existing file instead of overwriting it, we use the -a option:

$ script -a my_output.txt

For real-time monitoring, we can combine script with the -f option, while connecting it to a second terminal. This will enable us to print its output to a second terminal as it’s being recorded:

$ script -f /dev/pts/2

In the above example, let’s assume that the second terminal is /dev/pts/2. Then the output of the first terminal will show in the second one as well. The disadvantage here is that we have to plan the setup before execution.

These advanced features enhance the flexibility of script, making it suitable for a wide range of use cases in terminal session recording and monitoring.

5. Redirecting stdout to Another Terminal

Redirecting stdout (standard output) to another terminal is another useful and simple technique in Unix-like systems for monitoring output in real-time from a different session. This is particularly handy for debugging and keeping track of processes without interrupting the original terminal.

To redirect the output of a command to a file, we use the following syntax:

$ command > output.txt

For example, let’s again run a simple loop that counts from 1 to 100 and redirects the output to a file:

$ for i in {1..100}; do echo $i; sleep 1; done > output.txt

To view the output in real-time from another terminal, we use the tail command:

$ tail -f output.txt

We can also redirect the output directly to another terminal using the tee command. First, we need to find the device name of the target terminal. This can be done using the tty command in the target terminal:

$ tty

Suppose the target terminal device is /dev/pts/1. We can redirect the output as follows:

$ for i in {1..100}; do echo $i; sleep 1; done | tee /dev/pts/1

In this example, we start by running a loop that counts from 1 to 100, and we redirect the output to a file using the > operator. This captures all the output in output.txt. Next, we use the tail -f command to view the output in real-time from another terminal. Additionally, we can redirect the output directly to another terminal using tee by specifying the target terminal device.

These methods provide flexible ways to monitor and manage terminal output effectively, making it easier to debug and keep track of processes across different terminal sessions.

6. Using reptyr

The reptyr command is a unique tool that allows us to move a running process from one terminal to another. This is particularly useful when we need to keep a process running but want to close the original terminal or move the process to a more convenient terminal window. It’s also useful for managing processes during remote work.

To install reptyr, we use the following command:

$ sudo apt-get install reptyr

First, let’s start a long-running process, such as sleep 1000, in one terminal:

$ sleep 1000

Next, we can background the process with Ctrl-z and resume it in the background using the bg command:

$ bg

Next, we have to find the process ID (PID) with the jobs -l command:

$ jobs -l
[1]+ 12345 Running                 sleep 1000 &

Now, in another terminal, we can use reptyr to attach to the previously mentioned running process:

$ reptyr 12345

In this example, we start by running sleep 1000 in the original terminal, background it with Ctrl-z, and then resume it in the background using bg. We then find the PID of the process with jobs -l. Right after that, in another terminal, we use reptyr followed by the PID to attach to the running process. This allows us to manage and monitor the process in a new terminal.

7. Conclusion

In this article, we explored several powerful tools for terminal management in Linux. We used screen and tmux for creating and managing multiple terminal sessions, script for recording terminal sessions, and reptyr for transferring processes between terminals. These tools collectively provide a robust toolkit for managing Linux terminals locally or remotely, catering to our various operational needs.