A terminal is what differentiates a Linux user from a Windows user. Yes, there is the command prompt in Windows. But how reliable is it, and how many occasions are there for a user to actually use it? Not many. But in Linux, whether or not you may want to see the face of a terminal, you will have to, and I bet you’ll love the experience if you haven’t already used it yet.
Terminals are nice to play with, but every once in a while, there’s disorder. Inside a Linux terminal, you can press Ctrl + C to terminate a running process. However, there are times when it may not work, and you’d stay stuck like a lifeless iron, in line with your terminal. But brood not as you’re about to venture a journey of learning alternative ways to terminate a running process, when your Staff of Termination (Ctrl + C) is not working!
Why is Ctrl + C not working?
There can be a few distinct possibilities for Ctrl + C not working in your linux terminal. First of all, there are several programs available for the Linux Terminal, which have the ability to ignore and bypass terminal job controlling signals, not to mention, Ctrl + C which is used to interrupt a running process by means of sending a SIGINT signal. Programs like Vim and Ranger are able to do this.
On the other hand, the other common possibility for Ctrl + C not working is a wrong setting of STTY, where the keyboard shortcuts for assigning those signals are set. If for some reason, Ctrl + C is not set to the right option, or if it does not exist in the first place under stty, then you’ll not be able to run Ctrl + C.
Misconception of Ctrl + C to copy
A few users who’re new to Linux and the use of terminals face difficulties grasping the newness. In GUI programs, pressing Ctrl + C after selecting a string of text will allow you to copy that text to the clipboard. However, it does not work the same in linux terminals. Because Ctrl + C is already assigned for terminating programs. If you want to copy a string of text from the terminal, you’ll have to use Ctrl + Shift + C instead.
^C printing on screen instead of Ctrl + C
If the Ctrl + C is not set right in STTY, you’ll see “^C” being printed instead of terminating a program.
It is a verbose expression of “Ctrl + C”, which the terminal is able to interpret. However, if it is not set to do anything, the terminal will only ever print “^C”, until a shortcut is specified for it using STTY. To help you better understand the cause, try pressing Ctrl + A, which is probably not set for any function in terminals by default.
What you see is of no difference to Ctrl + C. This is because the terminal is able to take input when you press Ctrl + A, but it does not have a source to pass the input to, which is why, it rather gives you an output for its verbose expression, which is “^A”.
Using stty command to fix Ctrl + C
If you’re sure that Ctrl and C keys on your keyboard are working fine everywhere else other than the terminal, you can use the STTY method to fix Ctrl + C. First, try checking out whether you ^C (Ctrl + C) is assigned to a variable called intr, by typing the following command in your terminal:
$ stty -a
You should find a string that looks something like “intr = ^C;”, somewhere in the beginning, which depicts that Ctrl + C is assigned for interrupting (intr).
If you don’t find any expressions or such, it probably means that you’ll not be able to use Ctrl + C for terminating programs. To fix this, type the following command in your terminal:
$ stty intr [Ctrl + V][Ctrl + C]
Now hold on your horses. Don’t just copy this and paste it directly in your terminal, it won’t work. Because you need to actually press Ctrl + V followed by Ctrl + C on your keyboard, right after typing stty intr. If you’ve done that correctly, you should see ^C in the command:
Lastly, press Enter. Now try running it on the process where it didn’t work last time. Works now? Great. Now give me a gift 🙂
Using Ctrl + Z and kill to work like Ctrl + C
Unlike the SIGINT signal that is sent to the process when you press Ctrl + C, there are a few signals that no terminal program is able to ignore, not even Ranger or Vim. One of such signals is SIGSTP, which is sent when you press Ctrl + Z inside a running process. What’s different here is that instead of terminating the program directly like Ctrl + C, it rather pauses the process temporarily, and sends it to the background.
Once you see the STOPPED text in your terminal just like above, you can then use the following command to kill or terminate the process that was paused:
$ kill %1
This way even if Ctrl + C does not work for you for some reason, you’ll be able to use this method to do the job for you. Note that this will also work in programs, that’re by default able to ignore Ctrl + C.
Terminating processes from a separate terminal
If even the above method does not work in your specific case, there’s one more thing you could do, which is to terminate the process from a separate terminal. If you’re using a desktop environment in your Linux, then it is quite simple. Just start another terminal process, and run the htop program (make sure to have it installed if you haven’t already). Once you enter the htop program, search for the name of the process which you’re running in the other terminal. It shouldn’t be hard to find it. Once you get a sight of the process, press F9 on your keyboard, and then press Enter. You don’t have to worry about the list of signals that appear on the left hand side after pressing F9, as by default it is set to SIGTERM, which essentially means “Signal: terminate”.
However, things can be a bit more tricky if you’re using a Virtual Terminal (TTY). In that case, what you need to do is press Ctrl + Alt + F2 on your keyboard, which will bring you a separate virtual terminal, where you first need to login. Afterwards, you can start the htop program and follow the same procedure like the previous, to terminate the program – Enter htop -> Search for the process -> Press F9 -> Press Enter. Once done, you can quit from htop then navigate to the previous virtual terminal by pressing Ctrl + Alt + F1 this time around.
I can be rest assured that if you’ve tried out the above methods, you should be able to fix your issue for not being able to use Ctrl + C for terminating terminal programs. Now make sure you don’t hope Ctrl + C for copying texts you see on the terminal. And there’s a reason for this. Linux comes from old bones, the UNIX, where back in the days people only had the option to use a keyboard for everything, and quitting a program was not possible by clicking on the close button with your mouse like you can do now in GUI programs, which is why, Ctrl + C was set for the task instead. It could’ve been any other binding, but Ctrl + C in simple terms, is quite reachable and comfortable to press with your fingers, isn’t it?