How to close terminal with my program?
The problem is that my program do the scan, but the terminal remains open, i compile and run my program with termianl using: gcc auto.cpp -o auto.exe to compile, and ./auto.exe to run. I try to use system(«exit») but it doesn’t work. P.S. sorry for my bad english.
1 Answer 1
Although you can not make your compiled application close the terminal window in which it is running, it is easy to make this happen by changing the way how you execute your program.
For example, if your executable application can be started using ./myprogramm , you could just type the command below in your terminal to first run myprogramm and as soon as it finishes, run the exit command to close the shell and with it the whole terminal window:
Or maybe it would be even better to only close the terminal window if your application exited successfully, without any error (indicated by an exit status code other than 0). You can use && instead of ; to run the second command only if the first command was successful:
A third alternative would be to replace the shell running in your terminal window with your application, instead of running your application inside the shell. That way, the terminal window will close as soon as your application exits and you don’t have to exit the shell at that point any more:
One big difference between the first two approaches ( ; exit and && exit ) and the last one using exec is, that in the first case, you can kill the command you ran by hitting Ctrl + C and you will be back in the shell, the window will stay open. If you used exec , this will also kill your application, but as there is no more shell inside which it is running, the terminal window will close immediately as well.
Источник
How to close the Terminal?
I found many questions about how to open the terminal but cant find a question with an answer on how to close the terminal and return back to the desktop of Ubuntu (note: I am on virtual machine)
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 .
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 .
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
I started an installation process from a terminal and have two questions about this:
- How can I close the terminal without terminating the installation process? and how can I restore it again?
- How can I figure that this terminal has finished processing from another terminal so that I can do other processes based on the result of the first terminal?
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:
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.
Источник
How To Easily “Stop” a Terminal Command!
When you find yourself running a terminal command that you don’t know how to exit from. Don’t just close the whole terminal, you can close the that command!
If you want to force quit “kill” a running command, you can use “Ctrl + C”. most of the applications running from the terminal will be forced to quit.
There’s commands/apps that are designed to keep running until the user asks it to end. and there’s other commands/apps that are designed to show the output and exit automatically.
Here’s how to stop 3 of the most common commands.
Nano Editor
Nano is a simple text editor, you could have faced it when dealing with “git commit”.
If you’re inside nano editor. Press “ Ctrl + X” to exit. it will prompt you whether you want to save before quitting or not.
Vim Editor
Vim is another text editor, unlike nano, this one is sophisticated and powerful one.
If you were running Vim editor and you want to quit, you can press “ Esc” then type a colon “ :” followed by “ q!” to force quit without saving.
If you want to save do the same process but replace “ q!” with “ wq” (which means write then quit).
Less is a command that let you view the content of an input (either the output of another command or a content of a file).
Less is different from the editors above, if you’re inside commands that don’t need input, like “less” or top, you can press “ q”.
Others
Sometimes nothing from the above will work to quit the command gracefully. In these situations, you can use the “kill” command which is “ Ctrl + C”.
In general, try to quit the application gracefully so it can do what it’s designed to do when quitting. If it didn’t work just force quit (kill) it.
Источник
How to really clear the terminal?
I can issue the clear command or press Ctrl + L to clear the current Ubuntu terminal, but this just shifts previous output upwards and if you use mouse scroll or PgUP and PgDown keys it’s hard to distinguish where the output of previous command ends and output of current command begins.
Is there a way to really clear the terminal so I won’t see previous command results?
12 Answers 12
Yes, the command you’re looking for is
In contrast to clear , or Ctrl + L , reset will actually completely re-initialise the terminal, instead of just clearing the screen. However, it won’t re-instantiate the shell (bash). That means that bash’s state is the same as before, just as if you were merely clearing the screen.
As @Ponkadoodle mentions in the comments, this command should do the same thing more quickly:
From the other answers:
You can set a Keyboard Shortcut to reset the terminal, as explained by towolf.
If you’re running Kubuntu, and your terminal is Konsole, you need to go to Edit → Clear history, since reset doesn’t work the same way there, as UncleZeiv notes.
I was looking for this for a while and I found some genius that posted this:
Clears the whole screen buffer, very clean. Works on OS X and believe it works fine on most *nix terminals.
For curious, this part ‘\e[3J’ is a terminal escape command.
/.bashrc and add alias reset=»clear && printf ‘\e[3J'»
You can also assign a shortcut in gnome-terminal by going to Edit → Keyboard Shortcuts. I use Shift + Ctrl + Alt + C .
Cross posting my answer from stackoverflow.
Use the following command to do a clear screen instead of merely adding new lines .
yes that’s a ‘printf’ on the bash prompt.
You will probably want to define an alias though.
Explanation
So this becomes c which is the VT100 escape code for resetting the terminal. Here is some more information on terminal escape codes.
Here are a few other ways of doing it.
The above does not work on the KDE console (called Konsole) but there is hope! Use the following sequence of commands to clear the screen and the scroll-back buffer.
Or perhaps use the following alias on KDE.
I got the scroll-back clearing command from here.
run this command:
This has the same effect as launching a new terminal.
My favorite is printf «\ec» . This can also be printf «\033c» or printf «\x1bc» . That is an ansi escape sequence that miraculously clears the screen and buffer for the terminal output (for most standard terminals I have worked in it seems — I know it works in such as gnome-terminal, terminator, xterm, etc. on Ubuntu-like Linuxes)
I know this works in Linux Mint and Ubuntu 14.04, so I don’t know why people are appending and prepedning things like clear && echo -ne «\033c . printf «\ec» has always worked for me.
Additionally, in my .bashrc I have a binding like this:
Источник