Search code examples
bashshelltail

linux - running a process and tailing a file simultaneously


I want a run a long task on a remote machine (with python fabric using ssh). It logs to a file on the remote machine. What I want to do is to run that script and tail (actively display) the log file content until the script execution ends. The problem with

python test.py & tail -f /tmp/out

is that it does not terminate when test.py exits. Is there a simple linux trick I can use to do this or do I have to make a sophisticated script to continuously check the termination of the first process?


Solution

  • I would simply start the tail in background and the python process in foreground. When the python process finishes you can kill the tail, like this:

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