简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM