169

Can anyone tell me the difference between ctrl+z and ctrl+c?

When I am in the terminal, both the combinations stop the current process, but what exactly is the difference between both?

chaitanya lakkundi
  • 1,851
  • 2
  • 13
  • 9
  • 1
    I've asked a very similar question here: http://unix.stackexchange.com/questions/116959/there-are-stopped-jobs-on-bash-exit – ThorSummoner Aug 13 '14 at 17:09
  • 2
    An even closer match: [What’s different between Ctrl+Z and Ctrl+C in Unix command line?](http://superuser.com/q/262942/150988) on [SU]. – Scott - Слава Україні Aug 15 '14 at 16:11
  • yes i was successful in understanding the difference between both!! – chaitanya lakkundi Aug 16 '14 at 09:32
  • One thing I want to add here, That most of guys ( in my professional circle too ) do not differentiate between them. And used CTRL-Z option to terminate command. Which is wrong practice. You must use CTRL-C on all these instances. As CTRL-Z simply put in background. So process not terminated until completed, Which may not be desire of User (most of time) when he press CTRL-Z to terminate. – kuldeep.kamboj Aug 18 '14 at 06:44

6 Answers6

201

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 running fg (or %x where x is the job number as shown in jobs).

    We can test this by running nano TEST, then pressing Control+Z and then running ps aux | grep TEST. This will show us the nano 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.

Oli
  • 289,791
  • 117
  • 680
  • 835
  • 63
    It's worth adding that it's also possible to run `bg` (instead of `fg`) to unsuspend an application that's been Ctrl+Z'ed *without* putting it back into the foreground; effectively giving you control of both the shell that started the application *and* the application itself, as if you had used `&` when starting the application. This often comes in handy when you forgot to start it with `&` :) – Malte Skoruppa Aug 13 '14 at 14:10
  • 10
    And you can get back to that process by typing `fg` again! – sleblanc Aug 14 '14 at 03:15
  • 6
    In case there are several jobs suspended or in the background : "jobs" list them, and "fg %n" or "bg %n" or even "kill %n" to put job %n in foreground, background, or kill it. – Olivier Dulac Aug 14 '14 at 09:20
  • 3
    @Oli: Your second paragraph says, “The shell send[s] … signals to the underlying applications ….” No, the operating system / terminal driver sends the signals. (In a window system, the window manager may play a role.) Also, to be complete (although beyond the scope of the original question), you might want to mention Ctrl+\. – Scott - Слава Україні Aug 15 '14 at 23:08
  • How does the session know which process group has to return to the foreground after doing either `ctrl c` or `ctrl z` ? It defaults to the SID (bash)? – jiggunjer Sep 09 '16 at 12:29
  • @OlivierDulac, I tried to understand it by doing. But kill %1 said: Operation not permitted. The task is running with & at the end as seen in `jobs`. – Satya Prakash Oct 31 '17 at 07:28
28

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.

Zanna
  • 69,223
  • 56
  • 216
  • 327
nux
  • 37,371
  • 34
  • 117
  • 131
21

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

Bex
  • 228
  • 3
  • 12
  • 1
    Good job posting references. But generally speaking, Wikipedia is not a very good source for technical referencing. – Aaron Aug 13 '14 at 14:14
  • 3
    @BryceAtNetwork23 that is very true; I felt in this case the Wikipedia definition was decently sufficient. I will look for more technical references in the future though! – Sleep Deprived Bulbasaur Aug 13 '14 at 14:16
10

To put it simply:

  • CTRL-C requests that the program abort.

  • CTRL-Z suspends the program and it remains resident as a background task.

    Suspending a program allows you to resume it later with the command fg. Remaining background tasks are killed when you exit the login shell.

Note that CTRL-C only requests that a program abort, and the program may ignore the request. CTRL-Z will suspend the program unless the program specifically overrides the request.

thomasrutter
  • 36,068
  • 10
  • 86
  • 105
7

This should help

Ctrl+Z is used for suspending a process by sending it the signal SIGSTOP, which cannot be intercepted by the program. While Ctrl+C is used to kill a process with the signal SIGINT, and can be intercepted by a program so it can clean its self up before exiting, or not exit at all.

graham
  • 9,753
  • 18
  • 37
  • 59
  • 4
    This is not entirely correct. It sends `SIGTSTP`, which can be caught by the program. There are four different signals, which can suspend a program `SIGSTOP`, `SIGTSTP`, `SIGTTIN`, `SIGTTOU`. Of those only `SIGSTOP` cannot be blocked. The other three are used by the terminal to stop the process under different conditions. – kasperd Aug 15 '14 at 11:59
6

when you press ctrl+c, it means you send SIGINT to your process. like you type this command: kill -SIGINT <your_pid>. It will kill you your process. That why you cannot see it when issue ps command.
When you press ctrl+z, it means you send SIGSTOP to your process. like you type this command: kill -SIGKSTOP <your_pid>. It will stop your process, but the process still alive. So you can re-activate your process by sending SIGCONT to your process.

Benoit
  • 7,497
  • 1
  • 24
  • 34
P_O_I_S_O_N
  • 173
  • 8