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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with getting a Ctrl-C trap working w/ a piped tail -f...
# 1  
Old 04-11-2011
MySQL [Solved] How do you get 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?
Code:
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

Last edited by chatguy; 04-15-2011 at 02:29 AM.. Reason: [Solved]
# 2  
Old 04-11-2011
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:
Code:
(
  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:
Code:
   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

This User Gave Thanks to DGPickett For This Post:
# 3  
Old 04-15-2011
MySQL

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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

--killing backround Procs spawned from the parent script with Ctrl+C trap

Hello: Am trying to understand why the method #2 works but method #1 does not. For both methods, sending CTRL+C should kill both the Parent script & all of the spanwd background procs. Method #1: ========================== #!/bin/sh ctrl_c() { echo "** Trapped CTRL-C" ... (3 Replies)
Discussion started by: gilgamesh
3 Replies

2. Shell Programming and Scripting

Trap command not working

Hi Folks - For some reason, my trap command is not working. It's placed just prior to a normal exit: #:: ------------------------------------------------------------------------ #::-- Script Name: LCM_Backup.sh #:: #::-- Description: This script leverages Utility.sh to perform LCM... (16 Replies)
Discussion started by: SIMMS7400
16 Replies

3. Linux

ctrl+c not working

Hi All, I have ran one command tail -f <filename> to view newly appended text to that file. When i was done i pressed ctrl+c command to stop it but to my suprise it didn't work. I then tried top command and pressed ctrl+c and it worked fine and command aborted. I then just checked the tail... (0 Replies)
Discussion started by: tushar_shah06
0 Replies

4. Shell Programming and Scripting

stopping tail function after ctrl + c

say i have a statement like this in a script tail -f /opt/blah/blha/user.log > final.log if ;then cat final.log | grep -i "servicer_user" > service.log cat final.log | grep -i "logic_user" > logic.log fi echo "script completed" but when the script is running if i press ctrl + c the... (4 Replies)
Discussion started by: vivek d r
4 Replies

5. Shell Programming and Scripting

Trap CTRL-C and background process

Hello, I have a script which copies via scp several large files to a remote server. What I want is that even if someone hits CTRL-C, the scp commands continues till the end. Here is what I wrote #! /bin/bash function testFunction { echo "COPY START" scp large.tar.gz... (11 Replies)
Discussion started by: RobertFord
11 Replies

6. Shell Programming and Scripting

trap CTRL-C problem

I am trying to trap CTRL-C, now the program I call has it's own exit message, I think this is the problem .. This is what I have now : function dothis { echo 'you hit control-c' exit } function settrap { trap dothis SIGINT } settrap until false; do ./ITGRecv.exe doneDoing this I... (2 Replies)
Discussion started by: Pmarcoen
2 Replies

7. Shell Programming and Scripting

Trap not working in orphaned child processes

I've search the various posts in these forums, but have not come up with a solution to my problem. I have a parent process that calls a child script, runs it in the background and the parent finishes - without waiting for the child process to complete. Inside the child, a trap is issued to trap... (6 Replies)
Discussion started by: HobieCoop
6 Replies

8. UNIX for Dummies Questions & Answers

why tail +n not working?

Hello at my home when i try tail +5 emp it works but when i try in my college it doesn't work! what can be the problem any idea? when i type man tail it gives me various options in which it's mentioned that tail +n can also work when you want to display from nth line. But... (3 Replies)
Discussion started by: salman4u
3 Replies

9. UNIX for Advanced & Expert Users

trap ctrl c in shell script

how to trap the ctrl c in unix shell script my script is running in while loop it should not be terminate with ctrl c. if i press ctrl c while running script it shloud ignore the same. please healp.......... thanks in advance (2 Replies)
Discussion started by: arvindng
2 Replies

10. AIX

Disable ctrl-c,ctrl-d,ctrl-d in ksh script

I wrote a ksh script for Helpdesk. I need to know how to disable ctrl-c,ctrl-z,ctrl-d..... so that helpdesk would not be able to get to system prompt :confused: (6 Replies)
Discussion started by: wtofu
6 Replies
Login or Register to Ask a Question