bash: is there "ctrl-c" to kill just one command on multi commands line?
Hi,
Is there shortcut similar to "ctrl-c" that will kill just one command on multi command line? For example, if we execute the command: echo a; sleep 1; echo b; sleep 3; echo c; sleep 1; echo d; sleep 1; echo e And make a ctrl-c before "c" is echoed, the whole line is killed. I want it to kill the "sleep 3" command only, and the others after it would execute normally. Using bash's 'read' isn't really a solution because I would have to add a read between every line of every case... not easy, not pretty, seems like should exist something else. Any ideias? Thank you ---------------------------------- Tags: bash sh C-c ctrl-c |
Look here.
jdk |
control-c sends a signal to the program.
It is important to understand how individual programs react to those signals and how your script is going to react to those signals and exit/return codes. See "man 7 signal" and "man kill" Try writing some test programs/scripts which capture the signal by using a "trap". read "man -P 'less +2199' bash" and "man -P 'less +4613' bash" for an explanation. |
Not sure if this is exactly what you want, but it serves as a fun demo anyway...
Start this running: Code:
sleep 500; echo hello >> ttt; sleep 600; echo goodbye >> ttt --- To see this in action, do all the above in one window. But before starting, have a second window open running this: Code:
while [ 1 ]; do; sleep 1; ls -l ttt; done; Code:
while [ 1 ]; do; sleep 1; ps -ef | grep sleep; done; |
I'm not exactly sure what you want, but when I have a script executing a string of commands and one hangs up and I want to stop that particular command but not halt the script, I kill its process from another virtual terminal.
|
Sorry, people. Now I saw that my question was a bit unclear and misleading.
Thanks for the answer, Haertig. I wanted a key shortcut that works exactly like the ctrl-z does in your example. And in fact, ctrl-c does! LOL I even wrote a small C program to make sure the behavior with shell's commands (like sleep) wasn't different from normal programs. But I wasn't crazy when I wrote the question, although I was distracted to one VERY important detail: it was a shell script. My need is to send a "kill the current command" for .sh scripts, but let them continue. Typing a ctrl-c with aliases and functions kills just the current one! The second question: how to fully stop a function/alias? Let it quiet for now. So, the question is: can I kill the current command being executed inside a script? (a short and simple way, not using ps+kill bruteforce) Some tests I used now, that anyone can repeat: The nap.c program: Code:
// Compile with: gcc -Wall nap.c -o nap Code:
#!/bin/bash Code:
# ctrl-c kills a single one, you won't see "woke up" for it |
All times are GMT -5. The time now is 11:02 AM. |