What to do when Ctrl-C won't kill running job?

138,761

Solution 1

Send the SIGQUIT signal with Ctrl+\.

.. $ sleep 10
^\Quit

→ This is equivalent to kill -3 pid. Programs run in user-space don't ignore sigquit.

There is also a set of Magic SysRq keyboard shortcuts. One of the more interesting ones is this: Alt+SysRq+k. It kills every process on the current virtual console. If one of your ttys is completely and utterly broken, this can be used to go back. On the tty running X.org, it can be used to kill X without mercy.

The SysRq key is, on most keyboards, the same as the Print Key. On my notebook, it can be invoked using the Fn key; I.e. Alt→Fn→Print→k in that order.

Here are some of the basic process management shortcuts:

  • Ctrl+Z: pause a process (plus bg to resume in the background, fg to raise to foreground)
  • Ctrl+C: politely ask the process to shut down now
  • Ctrl+\: mercilessly kill the process that is currently in the foreground
  • Alt+SysRq+s: Write data to disk (always do this before killing anything important)
  • Alt+SysRq+s, k: mercilessly kill all current processes on a given virtual console
  • Alt+SysRq+s, b: mercilessly reboot without unmounting,
  • Alt+SysRq+r, e, i, s, u, b: Safely reboot even if the system is utterly broken,

Note: Ctrl+Z, in contrast to Ctrl+C and Ctrl+\, also works for man, vi, less, and the like.

When in doubt, the follwing procedure will almost always work:

~$ sleep 10
^Z
[5]+  Stopped              sleep 10
~$ ps
  PID TTY          TIME CMD
 4804 pts/0    00:00:00 bash
 6207 pts/0    00:00:00 sleep
 6208 pts/0    00:00:00 ps
~$ kill -9 6207
[5]+  Killed                  sleep 10
~$ 

^Z of course indicates that Ctrl+Z has been pressed.


For a more in-depth look at Shells and Terminals, also see my answers on:

Solution 2

You can try Ctrl+\

Solution 3

Sometimes the CTRL+C is getting sent to the wrong program or input channel. This is especially common with editors such as vi, with commands with piped output, complex bash commands involving loops, etc.

A simple, quick solution is to suspend the job CTRL+Z and then kill it by job number: kill %1 or kill -9 %1, etc.

Solution 4

  • Try one of these:

    Ctrl+4

    Ctrl+\


  • Or:

    Ctrl+Z and then run:

    kill %1

Where %1 is the number of job which was returned when you pressed Control-Z. Add -9 to force the kill.


When you're in text console, you could also try: Ctrl-SysRq. The kernel should kill the process.

Or SysRq-k, kill all processes on the current virtual console (can kill X and svgalib programs as well).

Note: When magic SysRq keys are used to kill a frozen graphical program, the program has no chance to restore text mode. This can make everything unreadable.

See: Magic SysRq key and its command combinations.

Solution 5

It's possible, that you changed the shortcut of copy from:

CTRL+SHIFT+C

To

CTRL+C

That way, when you try to kill a process, the CTRL+C doesn't work.

Try to replace the copy shortcut to CTRL+SHIFT+C.

Share:
138,761

Related videos on Youtube

Olivier Lalonde
Author by

Olivier Lalonde

Updated on September 17, 2022

Comments

  • Olivier Lalonde
    Olivier Lalonde over 1 year

    Sometimes, processes ignore the SIGINT signal sent by Ctrl-C in Bash. For instance, man and vi. Assuming you don't want to google or read the manual on how to exit the running process, is there any key sequence that will always work (without leaving the terminal)?

  • Olivier Lalonde
    Olivier Lalonde over 13 years
    Edited the question to make it clear I want to kill the process from within the terminal.
  • Olivier Lalonde
    Olivier Lalonde over 13 years
    Works for "sleep 10" but still doesn't work for "man" or "vi". Obviously "kill -9 pid" does work at killing both processes. Am I missing something?
  • flo
    flo over 13 years
    Ctrl+Z works with things like less, man, vi and so on. You can then kill them by doing a ps to see their process id and kill them using kill pid or, if you don't care about other instances of the program, killall vi. In any case, Ctrl-Z always seems to work.
  • Olivier Lalonde
    Olivier Lalonde over 13 years
    You can also do kill %% to kill the last job and kill %5 where 5 is the job number shown in jobs.
  • jenson-button-event
    jenson-button-event over 13 years
    Ctrl-\ sends a SIGQUIT, not a SIGKILL. There are some important differences between SIGQUIT and SIGKILL. en.wikipedia.org/wiki/SIGQUIT
  • flo
    flo over 13 years
    Juliano, I feel a bit silly now.. But i've edited the answer. Thank you!
  • Hippo
    Hippo over 13 years
    Doesn't work with either man or vi.
  • Anonymous
    Anonymous over 12 years
    Never ever do Alt+SysRq+b. Instead do Alt+SysRq+reisub to prevent damage to your system.
  • Chris Down
    Chris Down about 11 years
    There is no limitation that programs run in userspace have to honour SIGQUIT. C-\ does not "mercilessly kill the process that is currently in the foreground" any more than C-c does, both are trappable.
  • CMCDragonkai
    CMCDragonkai over 8 years
    On laptops without a print key, how do we get Sysrq? For Surface pro keyboards could it be Fn + Space?
  • CMCDragonkai
    CMCDragonkai over 8 years
    Does Alt+SysRq+r, e, i, s, u, b mean pressing r,e,i,s,u,b in sequence? And not holding it down.
  • flo
    flo over 8 years
    @CMCDragonkai Yes, it means holding down Alt and SysRq and then pressing r, e, i, s, u and b in sequence. The SysRq key could be anywhere on your keyboard, I'm not sure how you'd find out which one it is.
  • SuB
    SuB over 7 years
    Changing copy shortcut is irrelevant to force a process to be terminated!
  • AnotherKiwiGuy
    AnotherKiwiGuy over 7 years
    Can you please expand on your answer a little? Step by step would be ideal, primarily so the OP can follow along with your suggestion. :)
  • kenorb
    kenorb over 7 years
    @StefanoPalazzo Could you correct the broken images?
  • Forivin
    Forivin over 7 years
    How do I press Ctrl+\ when there is no \ key in my keyboard layout?
  • kRazzy R
    kRazzy R over 6 years
    worked ubuntu 16.04
  • dǝɥɔS ʇoıןןƎ
    dǝɥɔS ʇoıןןƎ over 5 years
    This actually helped me. I'm used to CTRL+C on MacOS, and didn't realise the Ubuntu command was CTRL+SHIFT+C
  • TetraDev
    TetraDev over 3 years
    I never realized that is what I did! Thanks. CTRL+SHIFT+C works
  • Alok Deshwal
    Alok Deshwal over 3 years
    can we catch CTRL+Z on node js using process.on('eventName')?
  • Jet Blue
    Jet Blue almost 3 years
    This exactly what I did ><! Thank you!
  • Chris J.T. Auld
    Chris J.T. Auld over 2 years
    How am I just learning about this?? +1+1
  • Vin Raghav
    Vin Raghav about 2 years
    None of these keys are working for me.