'tail -f' for a specific amount of time
With GNU timeout:
timeout 20 tail -f /path/to/file
For completeness sake, without timeout
, you can do this:
#!/bin/sh
tail -f /var/log/syslog &
me=$!
trap "kill $me" INT TERM HUP QUIT EXIT
sleep 20
The trap
line ensures that when the script or parent shell is terminated (we reach end of script (EXIT), Ctrl-C (INT), sending a SIGTERM via kill
, logging out of the shell (HUP), etc) then the tail
is killed.