Shell – How to start tailing a file that has not been yet created

filesshell-scripttail

I use tail to monitor the progress of jobs that I know will write their progress to disk. Almost always, I know which file they will create before they start running (the jobs are dispatched by a scheduler on a supercomputer)

Is there a way to tail these files before they are created? I would like to do so while avoiding race conditions and/or making assumptions about how or when
the jobs write to disk.

Best Answer

Use the -F flag to tail (assuming you have tail from GNU coreutils):

tail -F file-that-does-not-exist

From man tail:

   -F     same as --follow=name --retry
   --retry
          keep trying to open a file even when it is or becomes inaccessi‐
          ble; useful when following by name, i.e., with --follow=name
   -f, --follow[={name|descriptor}]
          output appended data as the file grows; -f, --follow, and --fol‐
          low=descriptor are equivalent
Related Question