What is the difference between Ctrl-z and Ctrl-c in the terminal?
If we leave edge cases to one side, the difference is simple. Control+C aborts the application almost immediately while Control+Z shunts it into the background, suspended.
The shell send different signals to the underlying applications on these combinations:
Control+C (control character
intr
) sends SIGINT which will interrupt the application. Usually causing it to abort, but this is up to the application to decide.Control+Z (control character
susp
) sends SIGTSTP to a foreground application, effectively putting it in the background, suspended. This is useful if you need to break out of something like an editor to go and grab some data you needed. You can go back into the application by runningfg
(or%x
wherex
is the job number as shown injobs
).We can test this by running
nano TEST
, then pressing Control+Z and then runningps aux | grep TEST
. This will show us thenano
process is still running:oli 3278 0.0 0.0 14492 3160 pts/4 T 13:59 0:00 nano TEST
Further, we can see (from that T, which is in the status column) that the process has been stopped. So it's still alive, but it's not running... It can be resumed.
Some applications will crash if they have ongoing external processes (like a web request) that might timeout while they're asleep.
Ctrl+C is used to kill a process with signal SIGINT
, in other words it is a polite kill .
Ctrl+Z is used to suspend a process by sending it the signal SIGTSTP
, which is like a sleep signal, that can be undone and the process can be resumed again.
However when a process is suspended, we can resume it again by fg
(resume in foreground) and bg
(resume in background) , but I can't resume a killed process, that is a difference between using Ctrl+C & Ctrl+Z.
How can we view suspended processes?
The jobs
command gives output like this:
[1]- Stopped cat
[2]+ Stopped vi
How to kill a suspended process in background?
By using the kill
command:
kill %n
where n
is the number displayed by the jobs
command. So if I want to kill cat: kill %1
.
Control+Z suspends a process (SIGTSTP
) and Control+C interrupts a process (SIGINT
)
http://en.wikipedia.org/wiki/Control-Z
On Unix-like systems, Control+Z is the most common default keyboard mapping for the key sequence that suspends a process
http://en.wikipedia.org/wiki/Control-C
In POSIX systems, the sequence causes the active program to receive a SIGINT signal. If the program does not specify how to handle this condition, it is terminated. Typically a program which does handle a SIGINT will still terminate itself, or at least terminate the task running inside it