Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
Doesn't kill -TERM work? – Hauke Laging May 15 '14 at 15:35
up vote 1 down vote accepted
trap 'kill -9 $CLOCK_PID' EXIT
tail -f mylog.log
share|improve this answer
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 '14 at 15:40
@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." – Hauke Laging May 15 '14 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

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.