I am writing a bash script to perform regular server maintenance. For this task, I am running a program that will take about 30 minutes to execute and will write to stdout every few minutes. I need the script to process these lines that the program writes in real time. I'm currently using a read-while loop to process each line like the following:
output=$($maintenance_command) while read -r line; do <processing logic> done <<< "$output"
and it does work properly, but it does not do any of the processing until the program exits. Is there any way to run this in the background and read the output as it is written?
until the program exits
, what program are you talking about? This$($maintenance_command)
?ls -1 | while read -r line ; do echo Line: $line ; sleep 1 ; done
. It's an example of using pipes and read the content line per line.|
will put the loop in a subshell, so any changes made to variables won't affect anything outside the loop. I'd suggestwhile read -r line ; do [processing logic] ; done < <($maintenace_command)
instead.stdbuf --output=L $maintenance_command | while ...
orunbuffer $maintenance_command | while ..
. I'm not sure if those commands will work. About the$maintenance_command
what programming language is using? or is it abash
script?