Jack
Jack

Reputation: 387

Bash redirection in a script in parallel

I have a bash script with a loop of processes that I want to run in parallel:

for i in {1..5}
do
echo Running for simulation $i
python script.py $i > ./outlogs/$i.log 2>&1 &
done

But when I do this the file redirection doesn't work, so $i.log just stays empty. The redirection only works when I do not use the & at the end, but then the script waits for each process to finish before starting the next one, which I don't want.

I tried a solution using script -c, but this does not update in realtime, only once the process ends. Does anyone have better suggestions, where the file redirection works in this script but it still updates in realtime?

Upvotes: 0

Views: 576

Answers (1)

Alexander Bocharov
Alexander Bocharov

Reputation: 201

You need simply add -u option so it will look like this:

python -u script.py $i > ./outlogs/$i.log 2>&1 &

Option -u is for unbuffered binary stdout and stderr

Upvotes: 1

Related Questions