-2

I have a bash script as given below: It runs the python script with different arguments, each one as a background process (note that I have used '&')

#!/bin/bash
declare -a arr=("arg1" "arg2" "arg3")
for i in "${arr[@]}"
do
    echo "$i"
    python3 test.py $i &
    echo "hi"
done
exit

The test.py file is as shown below:

import sys
print('Argument List:', str(sys.argv))

I tried to run the bash script with the command ./bash_script_test.sh. Output is also right, but the script just doesnt end running. Plus the python code's output starts in a new command line. Refer below for the output.

arg1
hi
arg2
hi
arg3
hi
[root@csit-openstack1 risav]# Argument List: ['test.py', 'arg2']
Argument List: ['test.py', 'arg3']
Argument List: ['test.py', 'arg1']

Why is a new command line coming up and why is the shell script not exiting? Is it because of the use of & ? If yes, can somebody explain?

techtie
  • 11
  • 4
  • the reason you're seeing the 'new command line (prompt)' is because the main script **has** finished and you are presented with the command line (prompt); the `py` output output is lagging and gets dumped to the terminal ... *in due time*; depending on the foreground/background processing speeds you could run your script repeatedly and see some of the output lines generated in a different order ... and in some terminals maybe even garbled on the same line; as you've figured out (per your comment - below), adding `wait` at the of the `bash` script will ensure the script really doesn't end ... – markp-fuso Sep 01 '21 at 14:40
  • ... until the backgrounded processes have completed, at which point you'll be presented with the command line (prompt); try this ... add a sleep to the `py` code, remove the `wait`, run your script and as soon as you see the command line (prompt) (ie, `bash` script has completed) then run `ls` ... you'll see your `ls` output and then at some point the `py` output will show up in your terminal – markp-fuso Sep 01 '21 at 14:40

1 Answers1

0

Take a cup of red color and a cup of green color and pour them into the same bucket. The result is a brown mess. The same happens with your terminal.

You have two processes, the foreground and the background process. Both write at the same time to the same terminal. The result is a mess. Background processes should write to log files instead.

Replace the line

python3 test.py $i &

with

python3 test.py $i > $i.log &

to give each background process its own log file.

If you want to merge the different sources, you have to use a tool like Syslog.

BTW: the script is ending. The last thing it does in the loop is printing "hi". And your output shows three times a "hi".

ceving
  • 21,900
  • 13
  • 104
  • 178
  • No the script doesnt end. I found a work around just now. I added 'wait' at the end of my bash script and now its not messy and the script ends. – techtie Sep 01 '21 at 13:12
  • @Rishmitha It is still messy, but you do not see it, because each sub-process prints just one line. When the sub-processes print more than one line, you will see it. And the script terminates. Put the following `trap 'echo I am done.' EXIT` in front of the `exit` and you will see it. And it is not necessary to wait for background processes, if you do not need to work on their output. – ceving Sep 02 '21 at 07:43