How to bind a key to sigkill in bash?
Asked Answered
S

3

11

I'm developing my application (on Linux) and sadly it sometimes hangs. I can use Ctrl+C to send sigint, but my program is ignoring sigint because it's too far gone. So I have to do the process-killing-dance:

Ctrl+Z
$ ps aux | grep process_name
$ kill -9 pid

Is there a way to configure bash to send the kill signal to the current process when I press - say - Ctrl+Shift+C?

Solubilize answered 4/12, 2008 at 8:25 Comment(0)
H
10

I don't think there is any key you can use to send a SIGKILL.

Will SIGQUIT do instead? If you are not catching that, the default is to core dump the process. By default this is ^\. You can see this by running:

$ stty -a

in a terminal. It should say:

quit = ^\
Heriberto answered 4/12, 2008 at 8:42 Comment(3)
How does this work? If I'm writing a new terminal application should I know about this or is it handled at a lower level?Commie
It is handled at the tty level in the kernel. When it sees the quit key, it sends a SIGQUIT to the foreground process on that terminal. You can catch SIGQUIT if you want to stop it, but core dumps on demand are quite useful.Heriberto
Here I was, like a total newbie, pressing ^ and \ to no effect. The actual buttons should be CTRL \ ... (at least on my machine).Superlative
C
7

You can send a SIGQUIT signal to the current foreground process by pressing ^\ (by default, you can run stty -a to see the current value for quit.)

You can also kill the last backgrounded process from the shell by running

$ kill %%
Cambodia answered 4/12, 2008 at 8:49 Comment(0)
C
2

Given that there's no bound key for SIGKILL, what you can do is to create an alias to save some typing, if SIGQUIT doesn't cut it for you. First, the

Ctrl+Z
$ ps aux | grep process_name
$ kill -9 pid

dance can be summarized (if there's only one instance of the process that you want to kill) as

Ctrl+Z
$ pkill -9 process_name

if your use case always goes to suspend then kill, you can create an alias to kill the last process ran like

$alias pks="pkill -9 !!:0"

Add that alias in your ~/.bash_profile.

Commie answered 4/12, 2008 at 8:35 Comment(2)
Instead of this you could use the kill % which kills the last jobDekko
On linux, killall process_name will kill all processes with that name. Caution: killall on Solaris and other unixes kills all the processes.Lineberry

© 2022 - 2024 — McMap. All rights reserved.