JavaSa JavaSa - 3 months ago 10
Bash Question

Running multiple scripts from bash on parallel without printing output to console

Let assume I have multiple files paths for run from terminal,
I want them to be run on parallel in the background without printing their output to the console screen. (Their output should be saved to some other log path which is defined in the python file itself).

The paths are in this format:

/home/Dan/workers/1/run.py
/home/Dan/workers/2/run.py etc.


When I try to do something like running one worker in background it seems to work:

In example:
cd /home/Dan/workers/1/

and
python run.py > /dev/null 2>&1 &


And
ps -ef |grep python
, indeed show the script running on background without printing to console, but printing to its predefined log path.

However, when I try to launch them all via bash script, I've no python scripts run after the following code:

#!/bin/bash
for path in /home/Dan/workers/*
do
if [-f path/run.py ]
then
python run.py > /dev/null 2>&1 &
fi
done


Any idea what is the difference?
In bash script I try to launch many scripts one after another just like I did for only one script.

cxw cxw
Answer
#!/bin/bash
for path in /home/Dan/workers/*
do         # VV    V-added ${}
    if [ -f "${path}/run.py" ]
    then 
        python "${path}/run.py" > /dev/null 2>&1 &
        # or (cd $path; python run.py > /dev/null 2>&1 &) like Terje said
    fi
done
wait

Use ${path} instead of just path. path is the name of the variable, but what you want when you are testing the file is the value that is stored in path. To get that, prefix with $. Note that $path will also work in most situations, but if you use ${path} you will be more clear about exactly which variable you mean. Especially when learning bash, I recommend sticking with the ${...} form.

Edit: Put the whole name in double-quotes in case ${path} contains any spaces.