Run programs in background and redirect their outputs to file in real time

The -u switch and the equivalent PYTHONUNBUFFERED environment variable forces stdout to be unbuffered. Try this:

#!/bin/bash
python -u 1.py > 1.output &
python -u 2.py > 2.output &
python -u 3.py > 3.output &

or

#!/bin/bash
export PYTHONUNBUFFERED=yes
python 1.py > 1.output &
python 2.py > 2.output &
python 3.py > 3.output &

Note that -u has side effects: read the doc to learn more.

Reference:

  • https://docs.python.org/2/using/cmdline.html#cmdoption-u
  • https://docs.python.org/2/using/cmdline.html#envvar-PYTHONUNBUFFERED