Printed output from Julia script

Julia does not seem to be printing the output as it appears when redirected to a file.
I have the following code in file print_test.jl:

for i in 1:10
     println("iteration: $i")
     sleep(1)
end

when I run the script like: julia print_test.jl the output is appearing as expected every second on the screen. But if I run the script like this: julia print_test.jl > test.out, the output will appear all at once only after the script is finished.
Is there a solution so the output would be written to the file gradually?
I am running a demanding Julia script on a cluster and I am monitoring the progress with println() function.

Interestingly, I observe the same issue if I submit the script to the queuing system and do not redirect to an external file:

#SBATCH --output=test.out
julia print_test.jl

My approach on SLURM clusters is to explicitly make a separate logfile and print to it. The stdout stream is buffered and also not always directed to the slurm output file. I have been meaning to look into the various logging packages to see if they make this any easier.

Edit: confirmed that this works, but after correcting capitalization of stdout:

I’m on my phone so can’t try this, but adding flush(stdout) after the println may help.

You could pipe through cat and the output will be unbuffered:

julia print_test.jl | cat > test.out

Or write to a file and simultaneously watch the output in the terminal:

julia print_test.jl | tee > test.out