4

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
  • 181
  • 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
  • I guess the problem is the same as trying to kill a program with kill <PID> you're trying to stop the program in an ordered manner. If you want to kill the program immediately you need to add -9 (the signal to stop a program immediately). You can test it pressing CTRL+Z, that will stop the process and get you back to the CLI prompt, you can then put the process in the background bg <PID> and then try to kill it, you will see you need the -9 or otherwise it will try to stop in an ordered manner and you will have to wait. – YoMismo Oct 10 '23 at 17:20
  • @YoMismo, yes, it is possible to choose "CTRL+Z" way but this is not what I needed. I needed to make CTRL+C work properly. It was working with the solution I found. You may give it a try if you have such issue too. – Kirby Oct 10 '23 at 18:14

2 Answers2

2

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
  • 181
  • 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
2

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
  • 242,166
mikem
  • 845