Baeldung Pro – Linux – NPI EA (cat = Baeldung on Linux)
announcement - icon

Learn through the super-clean Baeldung Pro experience:

>> Membership and Baeldung Pro.

No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.

Partner – Orkes – NPI EA (tag=Kubernetes)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

1. Introduction

When computers were first invented and made operational, they often occupied large rooms, even spanning entire floors of buildings. Users had to interact with them from a distance using input devices that resembled typewriters. These input devices were known as teletypes (TTYs).

The modern Linux system retains the TTY concept as an input channel to interact with the computer, but handles it virtually. So, software terminals are regarded as virtual teletypes, and a single computer can work with multiple TTYs.

In this tutorial, we’ll look at how we can start a process on a particular TTY by typing in the commands from a different TTY.

2. Setting up the TTYs

As a first step, let’s open two windows of a terminal emulator. Then, we’ll use the tty command in each terminal to identify its corresponding device name:

(terminal 1) $ tty
/dev/pts/1

(terminal 2) $ tty
/dev/pts/2

We can see that the tty command outputs different values for the two terminals, each appearing to be a filepath. In fact, the /dev directory in Linux is the device file directory, which lists all the virtual TTYs connected to the operating system.

Linux usually assigns a pts prefix to GUI terminal emulators, while command-line terminals accessed directly have a tty prefix.

In the following section, we’ll refer to these two terminals as pts1 and pts2.

3. Starting a Process in a Different TTY Using setsid

The setsid command stands for “set session id”, and we use it to run a process in a new session, detaching it from the current terminal. Most Linux distributions have this command preinstalled, so it’s preferable for simple use cases compared to other methods.

To start a process in pts2 from pts1, we can use the setsid command:

(terminal 1) $ setsid sh -c 'exec date <> /dev/pts/2 >&0 2>&1'

In this command, we use the -c option with sh to specify a shell command to run. We also include exec before the date command to replace the shell process with the date process. Lastly, we use >&0 to redirect stdout to stdin (which is now connected to /dev/pts/2), and 2>&1 to combine stderr with stdout.

After running this, we can see the output of the date command in pts2:

(terminal 2) Mon May 19 08:04:46 AM IST 2025

We see that the date is printed in the second terminal while we initiated the command on the first terminal. Finally, to avoid errors and unexpected results, we need to make sure that nothing else is running on the target TTY (pts2 in this case). This also includes a process that is waiting for a user to log in, such as a login shell.

4. Conclusion

In this article, we’ve explored how to start a process in a particular TTY from a different TTY. We used the setsid command to achieve this. It’s particularly useful when we want to start a command in a new session, detached from the current terminal.