Skip to content
Advertisement

Write background command output (stdout) to file during execution

I have a command that takes a long time that I like to run in the background like this:

JavaScript

This works perfectly in the sense that the command is indeed in the background and I can check the output and potential errors later.

The main problem is the output is only appended after the command is completely finished, whereas I would like to have up-to-date log messages so I check the progress.

So currently the log would be empty and than suddenly at 08:30 two lines would appear:

JavaScript

Instead, I would like to have output saved to file before the command is completely finished.

Advertisement

Answer

Since you are calling a Python script you would want to use the -u option, which forces the stdout and stderr streams to be unbuffered.

JavaScript

You can check the log periodically using cat or realtime in combination with watch:

JavaScript

https://docs.python.org/3.7/using/cmdline.html#cmdoption-u

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement