3

I'm struggling with not working CTRL+C in a certain environment shell command line (CentOS 7 / Vagrant guest of Windows host). I use bash there. The OS, seem, does not matter.

Example run sleep 1000 and press the ctrl-c:

$ sleep 1000
^C^C^C

So, it's typing ^C and that's it.

^C is bound for the interruption.

$ stty -a
intr = ^C; ... 

How to make it work?

In the following post, where I was inspired to fix it, the answer explains a lot but does not give a simple answer on how to make it work.
Why didn't Ctrl-C work?
It seems it's a simple thing that I'm struggling with.

Kirby
  • 161
  • 2
  • 9
  • What do you mean CTRL-C didn't work? Are you running a binary from the command line? Are you developing a script or prorgam and are asking how to trap the interrupt? Please elaborate on exactly what isn't working and give us a use case so we can better help you. – mikem Sep 13 '20 at 01:40
  • @mikem No, just command line, shell. I use bash It does not work for interacting a running script nor when you type and hit ctrl-c and it resets you to a new line. So, just shell CLI. If you got some idea how to explain it better, feel free to edit, plz. – Kirby Sep 13 '20 at 11:03
  • In my case Ctrl-Shift-C has become the way to stop a program. – user527366 May 26 '22 at 19:32

2 Answers2

1

As mentioned in How can we set up a signal trap to be SIG_IGN and SIG_DFL in bash? the simple solution is:

trap - INT

Just add this code into ~/.bashrc for bash, for example.

I decided not to find the root of the cause but just fix it.

Another interesting post: Remove ^C when CTRL+C

Kirby
  • 161
  • 2
  • 9
  • Interesting.. bash reads the following files on startup: /etc/profile ~/.bash_profile ~/.bashrc ~/.inputrc ... it this fixes smth similar for you, then my guess somewhere in there the INT trap gets unregistered there. – user56452 Sep 13 '20 at 19:46
  • @user56452, yeah, I was playing with these files by cleaning them. The same. Maybe not all of them... But at least the solution overrides it. – Kirby Sep 14 '20 at 12:03
1

It sounds like maybe a key-mappιng problem.

At the shell prompt, type:

stty sane

and then try to use CTRL-C to stop a command. If it still doesn't work, add the output from:

stty -a

to your question. The stty -a command will list your current terminal line settings. The item of interest is called intr. If intr is set incorrectly, CTRL-C won't work.

If intr is set incorrectly, you can try resetting it with:

stty intr CTRL-V-C

What that means is you type stty intr, then a space and then hold down the CTRL key and press v and then c with no spaces or or other characters between them.

After that, try to stop a command with CTRL-C again and see what the results are.

terdon
  • 234,489
  • 66
  • 447
  • 667
mikem
  • 806
  • 3
  • 8