Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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 &
CLOCK_PID=$!
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 at 15:35

2 Answers 2

up vote 1 down vote accepted
CLOCK_PID=$!
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 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." –  Hauke Laging May 15 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

 
discard

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.