Help with getting a Ctrl-C trap working w/ a piped tail -f...

Hi All,

Although each line below seems to work by itself, I've been having trouble getting the Control-C trap working when I add the "|perl -pe..." to the end of the tail -f line, below.
(That |perl -pe statement basically just adds color to highlight the word "ERROR" while tailing a log file)

...Does anyone know how to make the 2 lines below co-exist together, so that the CTRL-C trap still works, below?

trap 'print "\033[2K\033[41;1mUSER STOPPED LOG TAIL";' ERR
tail -f /var/log/logfile.log |perl -pe 's/ERROR/\e[41;1m$&\e[0m/g'

Thank you so much for the help,
CG

Well, signals go to all the processes, so the trap is in the tail's parent. You could open a subshell and set the trap just in there. It it is in a script, on most UNIX it is in a script-running subshell of the login shell. So, if you want the trap message to go up the pipe from the controlling shell, you probably need something like this:

(
  trap 'print "\033[2K\033[41;1mUSER STOPPED LOG TAIL";' ERR
  tail -f /var/log/logfile.log
 ) 2>&1 | perl -pe 's/ERROR/\e[41;1m$&\e[0m/g'

Also, Ctrl-C is SIGINT not SIGERR(?), see man kill:

   signum   signame   Name            Description
   ___________________________________________________________________________
      0     SIGNULL   Null            Check access to pid
      1     SIGHUP    Hangup          Terminate; can be trapped
      2     SIGINT    Interrupt       Terminate; can be trapped
      3     SIGQUIT   Quit            Terminate with core dump; can be trapped
      9     SIGKILL   Kill            Forced termination; cannot be trapped
     15     SIGTERM   Terminate       Terminate; can be trapped
     24     SIGSTOP   Stop            Pause the process; cannot be trapped
     25     SIGTSTP   Terminal stop   Pause the process; can be trapped
     26     SIGCONT   Continue        Run a stopped process
1 Like

DG,
Thank you so much! This works perfectly! Also thank you for the hint about using SIGINT instead of SIGERR. I changed this as well.

Thanks again for all the help,
CG