I have a script that tail a file while displaying a clock in the top right corner. I took the clock part from the internet, and it works ok. The entire script is something like (I simplified):

while sleep 1; do tput sc; tput cup 0 $(($(tput cols)-29)); date; tput rc; done &
tail -f mylog.log

All works well, but the problem is that when I stop the script using CTRL+C the clock continues to run in my console (until I manually kill the left-over bash process). So, is there a way to stop that process when I quit the script?

I tried the code below, but it does not work:

while sleep 1; do tput sc; tput cup 0 $(($(tput cols)-29)); date; tput rc; done &
tail -f mylog.log
kill -9 $CLOCK_PID

I searched how to trap CTRL+C in bash, but I'm not sure it's the right way...

  • Doesn't kill -TERM work? May 15, 2014 at 15:35

2 Answers 2

trap 'kill -9 $CLOCK_PID' EXIT
tail -f mylog.log
  • It works! Basically you have trapped the EXIT signal instead of the CTRL-C one? I think another error I did is that I put the trap after tail...
    – lorenzo-s
    May 15, 2014 at 15:40
  • 2
    @lorenzo-s There is no EXIT signal. That's a special term. man bash: "If a sigspec is EXIT (0) the command arg is executed on exit from the shell." May 15, 2014 at 15:42

Maybe watch will suit your needs. It prints the current time by default. Something like:

$ watch tail -n 30 /var/log/syslog

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.