2

I have a bunch of log files and I want to do a tail -f on them in a loop such that when I press Ctrl-C, the current tail -f gets killed and I proceed to the next log file:

for log in *.log; do
    printf '%s\n' "Tailing log '$log'; press Ctrl-C to skip to the next"
    tail -f "$log"
done

The issue is that pressing Ctrl-C kills the loop itself. How can I restrict the interrupt signal to just the child process, tail in this case?

1 Answer 1

2

Put

trap ":" SIGINT

before the loop. This makes the shell ignore the signal. But just in the sense that it executes a dummy command, not "ignore" in the signal handler sense.

Because the shell process does not block this signal (from the kernel perspective) it gets through to its child processes (like tail in this example).

3
  • This worked, thanks! Do you want to add how this works actually? Mar 13, 2018 at 22:41
  • Also, would this work I was running a built-in command instead of an external command like tail? Sep 4, 2018 at 19:36
  • 1
    @codeforester With the exception of wait built-in commands should finish before the shell reacts to the signal. But with few exceptions the built-in commands are executed so quickly that you would not notice anyway. Oct 16, 2018 at 20:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .