Tuyen Pham Asked: 2018-09-18 23:22:51 +0800 CST2018-09-18 23:22:51 +0800 CST 2018-09-18 23:22:51 +0800 CST 'tail -f' 持续一段时间 [重复] 772 我需要tail -f针对一个日志文件运行,但只运行特定的时间,例如 20 秒,然后退出。实现这一目标的优雅方法是什么? linux tail 2 个回答 Voted Best Answer cuonglm 2018-09-18T23:26:36+08:002018-09-18T23:26:36+08:00 使用 GNU 超时: timeout 20 tail -f /path/to/file Chris J 2018-09-19T04:21:37+08:002018-09-19T04:21:37+08:00 为了完整起见,没有timeout,您可以这样做: #!/bin/sh tail -f /var/log/syslog & me=$! trap "kill $me" INT TERM HUP QUIT EXIT sleep 20 该trap行确保当脚本或父 shell 终止时(我们到达脚本结尾 (EXIT)、Ctrl-C (INT)、通过 发送 SIGTERM kill、退出 shell (HUP) 等),然后将tail被杀死。
使用 GNU 超时:
为了完整起见,没有
timeout
,您可以这样做:该
trap
行确保当脚本或父 shell 终止时(我们到达脚本结尾 (EXIT)、Ctrl-C (INT)、通过 发送 SIGTERMkill
、退出 shell (HUP) 等),然后将tail
被杀死。