10

On my Linux host, Ctrl+C does not seem to work and I do not know how to proceed to make it work. I am using Ubuntu 10.04 with bash 4.1.5(1), and working in Gnome-terminal.

When I pressed Ctrl+C while this script was running, it did not cause it to quit.

#!/bin/bash
for i in `seq 500`
do
        ps -e > /dev/null
        echo $i
done
11
  • 3
    What do you mean by “does not work”? Where did you press that key, what did you expect to happen, what actually happened? Aug 11, 2011 at 22:42
  • Maybe you can describe further details of why you think that Ctrl-C does not work -- there are certain kernel level things which is not interruptible -- for example a process hanging waiting for a (failed) NFS answer can frequently not be interrupted
    – Soren
    Aug 11, 2011 at 22:49
  • I updated my answer. Basically Ctrl C did not terminate a script, that I think should be terminated on doing Ctrl C. Aug 11, 2011 at 23:01
  • Does stty -a output contain intr=^C? How do you run the script? Aug 11, 2011 at 23:04
  • 1
    Please do not cross-post questions. Choose the site you want to ask on, and tailor it to that site.
    – nhinkle
    Aug 11, 2011 at 23:09

7 Answers 7

9

Try to do this

$ stty sane

and see if it fixes the problem, also check if the intr is set correctly using

$ stty -a

If you find that intr is set to something different than ^C, then you can also fix it by typing

$ stty intr ^v^c
6
  • This did not help. stty -a shows "intr = ^C" ( without the quotes) Aug 11, 2011 at 21:51
  • Do I have to type the ^ character or press the Ctrl key? Aug 11, 2011 at 21:54
  • you just have to press the Ctrl key (^ is short for Ctrl-)
    – Soren
    Aug 11, 2011 at 22:47
  • Maybe you can describe further details of why you think that Ctrl-C does not work -- there are certain kernel level things which is not interruptible -- for example a process hanging waiting for a (failed) NFS answer can frequently not be interrupted
    – Soren
    Aug 11, 2011 at 22:49
  • I added more details to my question. Basically Ctrl C is not terminating a script that I am running, which I expect to be terminated on Ctrl C. Aug 11, 2011 at 23:32
8

The default settings in gnome-terminal binds the Ctrl-C and Ctrl-V keys to emulate the copy-and-paste behaviour of Windows. It was meant to be a feature not a bug :)

To disable this, at the gnome-terminal,

  1. navigate to Menubar -> Edit -> Keyboard Shortcuts
  2. click on the Ctrl+C in the Shortcut Key column until you see New Accelerator....
  3. press the Backspace or Delete key to disable the key binding
  4. verify that Disabled now appear in the Shortcut Key column.

Note that this change will be immediately effective on all gnome-terminals.

Tip: gnome-terminal --show-menubar will force gnome-terminal to show the Menubar if it is not shown.

0
6

Since it looks like a broken gnome-terminal configuration, you can try letting it restore the defaults using

mv .gconf/apps/gnome-terminal{,-BROKEN}

(You'll need to re-start the terminal to see the effects.)

If everything is fine after that, do a rm -rf .gconf/apps/gnome-terminal-BROKEN.

3

I'm going to take a guess here: Ctrl-C does work, but because ps -e takes a long time to run relative to the rest of the script, that all you do is send SIGINT to the "ps" process. "ps" exits, and the script picks right up with another iteration through the loop code.

Do a sleep 10 in your bash window, while it runs, control-C it. See if that works. If it does, run your original script, and control-C it multiple times. Sooner or later, you'll get lucky and the bash process that interprets the script will get the SIGINT.

2
  • Ctrl C on sleep 10 does not work either. Aug 12, 2011 at 3:02
  • 2
    So using gnome-terminal Ctrl C works but Ctrl c does not. So to interrupt sleep 10 if I do Ctrl + Shift + c it works. But doing Ctrl + c does not work. Why is it case sensitive. Aug 12, 2011 at 3:24
1

Check to see that you don't have CTRL+C already mapped as a Copy to Clipboard shortcut. If so, this will override the CTRL+C to quit that you're used to.

Linux terminals often come with SHIFT+CTRL+C as the default Copy to Clipboard shortcut and many users (including me) change this to CTRL+C as that's more intuitive coming from another OS.

Consider mapping the stop process shortcut to CTRL+X so you don't have to change your Copy to Clipboard muscle memory:

stty intr ^x
1
  • 1
    not sure why this answer was minused, its really good :)
    – Tom St
    Aug 2, 2020 at 12:39
1

When I looked at my shortcuts for terminal I see that the shortcut for "copy" is set to Control+C. So I changed it to Control+Shift+V (for copying).

Now everything is working.

Be sure to check

stty -a

and make sure intr is set to ^C before checking the shortcuts.

0

I know it is an old topic, but stupid mistakes have no age! ;)

I recently had changed CTRL-C to a shortcut for copying text and forgot about it.

After changing the shortcut for copy to SHIFT-INS now CTRL-C is working again in the terminal!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.