I need to run
tail -f against a log file, but only for specific amount of time, for example 20 seconds, and then exit. What would be an elegant way to achieve this?
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
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.