linux - 运行 一个进程同时跟踪一个文件

linux - running a process and tailing a file simultaneously

我想要一个 运行 远程机器上的长期任务(python 使用 ssh 的结构)。 它记录到远程机器上的一个文件。 我想要做的是 运行 该脚本并跟踪(主动显示)日志文件内容,直到脚本执行结束。

的问题
python test.py & tail -f /tmp/out

是在test.py退出的时候不终止。 我可以使用一个简单的 linux 技巧来执行此操作,还是我必须制作一个复杂的脚本来连续检查第一个进程的终止?

我会简单地在后台启动 tail 并在前台启动 python 进程。 python 进程完成后,您可以终止 tail,如下所示:

#!/bin/bash

touch /tmp/out # Make sure that the file exists
tail -f /tmp/out &
pid=$!
python test.py
kill "$pid"