1

I have 2 related Doubts about Bash.

(Q1) Consider tail -f SomeFile | wc, a fictitious command-line, where tail is used to simulate a command (C1) which runs for a long time, with some output from time to time, and wc is used to simulate a command (C2) which processes that output when C1 finishes. After waiting for a long time (longer than usual) I want to see what output has been generated till now, so I want C1 to terminate. But pressing Ctrl-C will kill this whole pipeline. How can I kill only C1 ( or even a component of C1, if that is itself a compound command ) ?
If C1 had been looping over many files and grepping some text, but one file was from some hung nfs server, then I want to kill only that grep process.

(for F in A B C D E ; do grep sometext $F ; done) | wc

Here Ctrl-C will kill the whole command-line, but I want to kill only the currently running (or hung) process and continue with remaining files.

One solution I have, is to open a new connection, get the ps output, and "kill" it. I was wondering if there was a solution from Bash itself, such that some strange key-combination kills only the current process ?

(Q2) While trying to make examples for this question, I made this command-line,Here, where if I press Ctrl-C, I get an extra line of output, like this:

# echo `ping 127.0.0.1` | wc
^C

#

When backticks (``) are not used, the extra line is not there:

# tail -f SomeFile | wc
^C
#

Am I correct in thinking that since backticks (``) are handled by bash itself and, when the sub-process is killed, it is still considered as "empty output", so that is printed as the extra line ?

3 Answers 3

2

In bash you can run:

cmd1 | cmd2 | (trap '' INT; cmd3)

And a Control-C will only kill cmd1 and cmd2, but not cmd3.

Example:

$ while sleep .1; do echo -n 1; done | (trap '' INT; tr 1 2)
^C222222222
$ while sleep .1; do echo -n 1; done | tr 1 2
^C

This takes advantage of the fact that a signal disposition of "ignore" is inherited by subprocesses -- the trap '' INT will also affect the tr command. But of course, some commands install their own SIGINT handlers, which will break this assumption.

Unfortunately, this doesn't work in ksh93 because of a stupid bug. A workaround there could be:

ksh93$ while sleep .1; do echo -n 1; done | sh -c 'trap "" INT; exec tr 1 2'
^C222222222ksh93$
1
  • Nice description of what happens is here unix.stackexchange.com/a/407254/383162 (all commands will receive SIGKILL, but when some program tries to output something to pipe, which is already closed, it will additionaly receive SIGPIPE...)
    – Lubo
    Nov 5, 2020 at 21:21
0

The command stty -a will show you all keyboard shortcuts. The only signals mapped are Ctrl-C (SIGINT), Ctrl-\ (SIGQUIT) and Ctrl-Z (SIGSUSP). There is no binding to other signals.

I would use the solution that you yourself said: separate shell, psoutput and killthe process you need killed.

To your second question, it is not because of the grave accents (``), but because of echo. Try this example:

# echo `sleep 10` | wc
^C

# `sleep 10` | wc
^C
# sleep `echo "10"` | wc
^C
#
-1

Tested with below command and it worked fine

tail -f /var/log/kern.log & read -t 10 ;kill $!

Above command will work for only 10 seconds and then get terminated.

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.