Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force flushing of output to a file while bash script is still running

Tags:

file

bash

flush

I have a small script, which is called daily by crontab using the following command:

/homedir/MyScript &> some_log.log 

The problem with this method is that some_log.log is only created after MyScript finishes. I would like to flush the output of the program into the file while it's running so I could do things like

tail -f some_log.log 

and keep track of the progress, etc.

like image 433
olamundo Avatar asked Sep 15 '09 22:09

olamundo


People also ask

Can you edit a bash script while it is running?

with the current version of bash, modifying a script on-disk while it is running will cause bash to "try" to load the changes into memory and take these on in the running script. if your changes come after the currently executing line, the new lines will be loaded and executed.

How do I flush stdout in Linux?

Use the fflush Function to Flush stdout Output Stream in C As a result, there are buffers maintained by the C library for handling the input/output operations when using the stdio function calls. If the user needs to force writing to kernel buffers, it needs to flush the given stream provided by the fflush function.

How do I force stop a bash script?

There are many methods to quit the bash script, i.e., quit while writing a bash script, while execution, or at run time. One of the many known methods to exit a bash script while writing is the simple shortcut key, i.e., “Ctrl+X”. While at run time, you can exit the code using “Ctrl+Z”.

Does a bash script wait for command to finish?

The bash wait command is a Shell command that waits for background running processes to complete and returns the exit status. Unlike the sleep command, which waits for a specified time, the wait command waits for all or specific background tasks to finish.


2 Answers

I found a solution to this here. Using the OP's example you basically run

stdbuf -oL /homedir/MyScript &> some_log.log 

and then the buffer gets flushed after each line of output. I often combine this with nohup to run long jobs on a remote machine.

stdbuf -oL nohup /homedir/MyScript &> some_log.log 

This way your process doesn't get cancelled when you log out.

like image 188
Martin Wiebusch Avatar answered Sep 22 '22 04:09

Martin Wiebusch


script -c <PROGRAM> -f OUTPUT.txt 

Key is -f. Quote from man script:

-f, --flush      Flush output after each write.  This is nice for telecooperation: one person      does 'mkfifo foo; script -f foo', and another can supervise real-time what is      being done using 'cat foo'. 

Run in background:

nohup script -c <PROGRAM> -f OUTPUT.txt 
like image 26
user3258569 Avatar answered Sep 20 '22 04:09

user3258569