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:

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?

  • About until the program exits, what program are you talking about? This $($maintenance_command)? Oct 18, 2022 at 4:16
  • Yes, correct. The script does not start going through the loop until $($maintenance_command) exits.
    – Trevor
    Oct 18, 2022 at 4:29
  • 1
    Try running something like this in your terminal: 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. Oct 18, 2022 at 4:33
  • 2
    Using a pipe with | will put the loop in a subshell, so any changes made to variables won't affect anything outside the loop. I'd suggest while read -r line ; do [processing logic] ; done < <($maintenace_command) instead.
    – frabjous
    Oct 18, 2022 at 4:51
  • 1
    @Trevor if process substitution doesn't work then try: stdbuf --output=L $maintenance_command | while ... or unbuffer $maintenance_command | while ... I'm not sure if those commands will work. About the $maintenance_command what programming language is using? or is it a bash script? Oct 18, 2022 at 5:24

1 Answer 1


I was testing this script by using a python script that would output the same lines as the real program. However this behavior did not happen with the real program so must have been something with the python script.

  • Hi! Please, instead of creating an answer, edit the original question adding the new info.
    – Luigi T.
    Nov 2, 2022 at 11:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .