What happens when you Ctrl-c in the terminal
It happens quite often to me that I run something in the terminal and then I need to stop it (quit) and I use the Ctrl-c
key combination for that. I never really knew what’s happening under the hood at that moment and how this key combination works.
Turned out the way Ctrl-c
works is quite simple — it’s just a shortcut key for sending the interrupt (terminate) signal SIGINT
to the current process running in the foreground. Once the process gets that signal, it’s terminating itself and returns the user to the shell prompt.
What about Ctrl-z
?
I found another interesting and probably useful key combination Ctrl-z
. This combination sends SIGTSTP
signal to the process (that is running in the foreground) and basically pauses the process and returns the user to the shell prompt.
If the process is paused, there should be a way to resume it, right? And the simplest way to do it is to run fg %1
command. It will bring back the program to the foreground and continue it from the moment it was stopped at. I used it when running long unit-tests and it worked exactly the way it claims, it’s amazing!
Other commands to manage shell processes
There are some more basic shell commands to manage the processes:
ps
to list all the processes running on the systemjobs
to list processes managed by the current shellbg
to resume suspended (paused) jobs in the backgroundkill
to send any signal to a process
As with all shell commands, the commands above accept different options that change the command behavior. For example, the processes, listed by pc
, and the amount of information shown depends on the options you pass to pc
command.