When using nohup
the output of a script is buffered and only gets dumped to the log file (nohup.out) after the script has finished executing. It would be really useful to see the script output in something close to real-time, to know how it is progressing.
Is there a way to make nohup
write the output whenever it is produced by the script? Or, since such frequent file access operations are slow, to dump the output periodically during execution?
Asked
Active
Viewed 3,025 times
4

feedMe
- 3,431
- 2
- 36
- 61
-
Following the perfect answer from @john-zwinck here is the working solution (for a python script): `nohup unbuffer python script.py > log &` – feedMe Jan 30 '15 at 13:23
-
1This is not working for me... :-( Gives the error "nohup: failed to run command 'unbuffer': No such file or directory" @john-zwinck have things changed? – Selfx Aadhyant Mar 19 '19 at 03:38
-
@SelfxAadhyant You need to install expect as mentioned by **brezniczky** in https://askubuntu.com/questions/1047900/unbuffer-stopped-working-months-ago – Kaushik Acharya Jan 26 '20 at 18:08
3 Answers
5
There's a special program for this: unbuffer! See http://linux.die.net/man/1/unbuffer
The idea is that your program's output routines recognize that stdout is not a terminal (isatty(stdout) == false
), so they buffer output up to some maximum size. Using the unbuffer
program as a wrapper for your program will "trick" it into writing the output one line at a time, as it would do if you ran the program in an interactive terminal directly.

John Zwinck
- 239,568
- 38
- 324
- 436
-
Thanks very much. Interesting how the syntax is ordered with `unbuffer` coming *after* `nohup`. I commented on my own question with the solution as I ended up using it. – feedMe Jan 30 '15 at 13:27
1
What's the command you are executing? You could create a .bash file which inside is redirecting the output to files after each command (echo "blabh" >> your-output.txt) so you can check that file during the nohup execution (you should run: nohup script.bash &)
Cheers

Cris R
- 1,339
- 15
- 27
-
OK this would work but I want a more elegant solution. I would have to include a lot of print to file statements in my code to see all that is happening. Of course, my script is writing some data files anyway so I am not completely blind to the process. – feedMe Jan 30 '15 at 13:01
-
just consider using "Line Buffering" instead of "No buffering".... stdbuf -i0 -oL -eL – lucky62 Sep 07 '21 at 09:27