- Is there a command to close a terminal window via commandline?
- How to Close a TTY?
- 1. Introduction
- 2. Terminal Process
- 3. Closing Terminals
- 3.1. Exit the Shell
- 3.2. TTY Allocation and Deallocation
- 3.3. Reduce Available TTYs
- 4. Summary
- How to close the Terminal?
- 3 Answers 3
- close the terminal without terminating the process
- 1 Answer 1
Is there a command to close a terminal window via commandline?
To close a terminal window you can use the exit command . Alternatively you can use the shortcut ctrl + shift + w to close a terminal tab and ctrl + shift + q to close the entire terminal including all tabs.
You can use the ^D shortcut — that is, hitting Control and d. This works in many shells and shell-like environments. Technically speaking, this key combination emits the end-of-transmission character (ASCII code 04). In gnome-terminal , i.e. bash and other shells, this is interpreted as finishing the interactive session. You can also see this behavior in programs like cat which read from standard input (the keyboard). Try running it without arguments; you can terminate by entering ^D .
is the command you want to use for that.
There is an alternate way, which requires a bit more keystrokes, but is worth knowing.
Using exec command
which would, effectively, close the window after 1 second.
It doesn’t quite «exit» the shell, but it rather replaces the shell’s process with the
I know this answer is a little bit late but, If you are using Ubuntu or another Linux distro, simply use Ctrl + D to close the entire terminal.
I initially tried all the rest and they were just not working. For example I see people recommending exit but honestly, it just doesnt work.
I hope someone agrees with me.
Ctrl + D works indeed, but in my case «exit» has always been working too. It’s weird that it doesn’t for you.
xdotool getactivewindow windowkill
Notice that this will kill all active windows!
If you want to kill only the current window use:
xdotool key —clearmodifiers Ctrl+Shift+Q key —clearmodifiers KP_Enter
Welcome to AskUbuntu. A good answer that can be improved. «Try using» is the hallmark of a suggestion and suggestions should go in comments. Replacing it with «You can use» makes it into a proper answer. I’ll suggest an editing.
Yes there is.First type echo $$ in terminal to know it’s process id. Then type kill -9 processid .
To close the terminal whilst leaving all running processes to continue running, you can do the following:
This will leave the current running process running in the background and close the terminal(i find this to be very useful).
Yes. The command is exit. Type exit into the terminal and press enter
I’m using KDE here. I wrote a script and I execute it by right click > execute in console. After the executtion, the newly opened window of konsole, was left open and I had to manualy close it (by pressing X or Alt+F4, or typing exit). I combined two of your answers here (THANKS to the posters) and now the window closes automatically.
#!/bin/bash # echo Hello & disown sleep 1 echo Closing in 3 sleep 1 echo Closing in 2 sleep 1 echo Closing in 1 sleep 1 xdotool getactivewindow windowkill
Without the & disown and xdotool getactivewindow windowkill, just by using «exit» at the end, nothing happened! Thank you again!
How to Close a TTY?
The Kubernetes ecosystem is huge and quite complex, so it’s easy to forget about costs when trying out all of the exciting tools.
To avoid overspending on your Kubernetes cluster, definitely have a look at the free K8s cost monitoring tool from the automation platform CAST AI. You can view your costs in real time, allocate them, calculate burn rates for projects, spot anomalies or spikes, and get insightful reports you can share with your team.
Connect your cluster and start monitoring your K8s costs right away:
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 PID TTY CMD 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 Password: $
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:
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 PID TTY CMD 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 /dev/tt1 $ 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 PID TTY CMD 668 tty1 agetty
On the many distributions with the standard systemd, we can simply use systemctl to stop the relevant TTY allocation service:
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 PID TTY CMD
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 PID TTY CMD 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:
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.
How to close the Terminal?
In effect this question, taken together with the OP’s comments below, has two parts: figuring out the kind of terminal, then getting out of it accordingly. There’s also the potential problem of key combinations being applied to a host (physical) system rather than the guest (virtual) system. But I think we can make this a duplicate of How do I get to the GUI after switching to a virtual console?, I accidentally hit Ctl Alt F. How do I return to the GUI?, or Reverting from Ctrl — Alt — F1.
It should be made clear whether OP is concerned about returning from a terminal emulator to desktop or from a tty to desktop. For terminal emulator enter exit and for tty press ctrl + alt + f8 or alt + f8.
@Faizan I think that comment by the OP, taken with the question, pretty strongly indicates the goal is to get back to the GUI from a text-based virtual console. The OP doesn’t seem to have been back in months, but I agree clarification from them remains welcome. Btw, on Ubuntu, X11 nearly always runs on tty7 —so Alt+F7, rather than Alt+F8. (I’ve heard many Mint systems have X11 on tty8 , but I don’t know if that’s true as my only Mint system is LMDE and not the more common Ubuntu-based Mint.)
3 Answers 3
You can type exit . You can type ctrl — d . Or (if you’re on a virtual terminal), you use ctrl — alt — F8 .
my terminal in virtual machine is at a black background and when i type exit it refreshes its not a program opened in ubuntu like i see in videos what can i do & is there a program that let me have the terminal as a openable program in the ubuntu
I sometimes notice that after trying to log out with crtl+d the log takes longer or the window doesn’t close, pressing ctrl+c usually helps (and if we’re talking about ssh using the parameter -v).
ctrl + shift + w closes the current tab and ctrl + shift + q closes the entire window.
This is similar to other commands that also are normally ctrl + whatever such as, ctrl + c and ctrl + v for copy and paste which, in gnome-terminal are, ctrl + shift + c and ctrl + shift + v respectively.
I’m not sure if the following works in a virtual machine, however, it is important to also note that when you switch to a tty text session such as tty1 — tty6 using ctrl + alt + f1 up to ctrl + alt + f6 , you can return to the regular desktop xsession by pressing ctrl + alt + f7 typically or sometimes ctrl + alt + f8 .
We have a question about switching terminals in a guest system (virtual box) askubuntu.com/q/49571/40581 🙂
TL;DR: Press Alt + F7 .
You are apparently asking how to close a virtual console, also known as a virtual terminal—a full-screen text terminal you got to by pressing a key combination like Ctrl + Alt + F1 , Ctrl + Alt + F2 , and so forth.
In infer this since you say you wish to «return back to the desktop of Ubuntu» (indicating you are not on your desktop now) and, in a comment, wrote:
my terminal in virtual machine is at a black background and when i type exit it refreshes its not a program .
Rather than actually closing your text-based virtual console, you probably just want to get back to the virtual console on which the graphical user interface is running. This is usually the seventh virtual console, so press Alt + F7 . ( Ctrl is optional when switching from a virtual console that does not have the GUI running on it; however, you must use it when switching from a virtual console that does have the GUI running on it.)
If those key combinations do not work—or change the virtual console of the host rather than the guest—and your Ubuntu system is a VirtualBox guest system, see How do I switch between virtual terminals in a guest system? as suggested by LiveWireBT.
Another way, which is much less often used, is to run the command chvt 7 .
close the terminal without terminating the process
For 1, see askubuntu.com/q/106351/158442. There’s no easy way to restore if you haven’t used screen or tmux before starting the process. 2. Check whether the process is still running using pgrep , top or some other process monitoring tool.
A similar question with various good answers and lots of votes can be found at unix.stackexchange.com/q/4004/163108
1 Answer 1
For 1. you need to send your running process to the background and remove the associated job from current shell.
- Press Ctrl + Z and type bg to send the installation process to the backgroud
- then type disown .
You can now close the terminal, the process will still be alive. You can open another terminal and check its process id with ps -aef
In my case the process id is 14426 . Unfortunately there’s no easy way to reattach it to another terminal (See How to attach terminal to detached process?) unless you used something based on screen .
For 2. You can use the following command:
while kill -0 14426 >/dev/null 2>&1; do sleep 5 ; done ; echo "ok"
It will print ok when the process is over from an other terminal. You can of course change this echo command with something more complex.