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"

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?



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).

  • This worked, thanks! Do you want to add how this works actually? – codeforester Mar 13 '18 at 22:41
  • Also, would this work I was running a built-in command instead of an external command like tail? – codeforester Sep 4 '18 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. – Hauke Laging Oct 16 '18 at 20:12

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.