1. Introduction

Like other processes in a Linux system, terminals take up resources. Because of this, we might sometimes want to prevent an unused terminal from running.

In this tutorial, we explore ways to close a terminal. First, we understand the terminal process in general. After that, we follow the stages of closing a terminal.

For brevity, in this tutorial, we:

  • use the terms terminal and TTY interchangeably
  • minimize the output of some commands to the useful essentials

We tested the code in this tutorial on Debian 11 (Bullseye) with GNU Bash 5.1.4. It should work in most POSIX-compliant environments.

2. Terminal Process

A TTY is a gateway (process) to the machine that the operating system (OS) provides to the user:

$ ps -H -t /dev/tty1
660 tty1  agetty

In this state, the ps [-H]ierarchy shows us that [-t]erminal /dev/tty1 is simply a reservation by a TTY maintenance mechanism.

Later, the shell that runs over that terminal plays a vital role when it comes to leveraging control functions. In fact, the login shell is the first official point of contact with a TTY:

baeldung login: user

At this point, we see the prompt of a shell running inside a terminal. Let’s check which terminal that is with the tty command:

$ tty

In this case, we use /dev/tty1. Of course, we can switch the TTY or use some higher-level virtual terminal like a PTY.

In all cases, closing a terminal is similar and may depend mainly on the initialization process.

3. Closing Terminals

Unlike when killing a terminal, e.g., when a TTY breaks, we aim to perform a smooth close operation.

3.1. Exit the Shell

Let’s check the current hierarchy of our TTY:

$ ps -H -t /dev/tty1
666 tty1  login
667 tty1   bash

One of the most natural ways to get out of a terminal session is to close its shell with a command like exit:

$ tty
$ exit
baeldung login:

As the main element of the TTY, exiting the shell frees up most resources related to the given terminal. Still, the main allocation process remains.

3.2. TTY Allocation and Deallocation

After stopping the shell, we’re back to the initial TTY:

$ ps -H -t /dev/tty1
668 tty1  agetty

On the many distributions with the standard systemd, we can simply use systemctl to stop the relevant TTY allocation service:

$ systemctl stop [email protected]

The process is similar to the SystemV init but involves the handling of links and /etc/inittab.

After the above, we can use another terminal to verify nothing is running for /dev/tty1:

$ ps -H -t /dev/tty1

The terminal is now closed. In fact, stopping the terminal maintenance service before ensuring a smooth exit for the shell and all its child processes will terminate all of them abruptly.

3.3. Reduce Available TTYs

As long as we don’t attempt to switch to /dev/tty1, the terminal will have no associated processes. However, as soon as we press CTRL+ALT+F1 or otherwise open that TTY, the system restarts the allocator:

$ ps -H -t /dev/tty1
669 tty1  agetty

Actually, the reason for this is simple – terminals get allocated on demand. Moreover, configuring the way it’s done and the TTY limit happens according to the distribution.

For example, the older SystemV init uses /etc/inittab and /etc/init/tty*.conf to configure terminal allocation. On the other hand, the newer SystemD provides options in /etc/securetty and /etc/systemd/logind.conf*. What’s more, Berkeley Software Distribution (BSD) has /etc/ttys.

Once the desired configuration is in place, we can run deallocvt to ensure no resources are taken up by extra terminals:

$ deallocvt

Now, we have completely eliminated any leftover resources by any unused (not in the foreground, no selected text, not open by any process) terminal.

4. Summary

In this article, we saw how to close a terminal on several levels.

In conclusion, we can simply close a TTY by exiting its shell. Depending on our needs, lower-level options also exist.

Comments are closed on this article!