0

I made shell script to run my task with many seeds. Here is my shell script

task.sh

for i in 1 2 3 4 5
do
    setsid python main.py --random-seed 24 1>data/${i}_24.log 2>&1
    sleep 3
done
echo "OK 1"


for i in 1 2 3 4 5
do
    setsid python main.py --random-seed 48 1>data/${i}_48.log 2>&1
    sleep 3
done

echo "OK 2"

for i in 1 2 3 4 5
do
    setsid python main.py --random-seed 60 1>data/${i}_60.log 2>&1
    sleep 3
done

echo "OK 3"

However, the first process with seed 24 with i=1 has been launched and the following process had not been launched. I think the reason may be in shell script it only supports running process one by one. How can I fix it to run all the process in background?

GoingMyWay
  • 16,802
  • 32
  • 96
  • 149
  • 1
    `myCmdline with args &` will run that command in the "background" allowing the next line in your script to be executed. But you probably don't want all of your loops executing at once, if there is any chance of not enough CPU or other resource contention. Good luck. – shellter Jul 23 '19 at 04:01
  • @shellter I added `&` and it works. I want to run all the processes at once. But why? When I run these for loops in the command line, it can work, in shell script, it can not run all the processes simultaneously in the background. Since with `setsid`, it will run a process in the background. – GoingMyWay Jul 23 '19 at 04:04
  • can you show a sample of a python script, bash totally depend upon the return code from python – Adiii Jul 23 '19 at 06:06
  • after launching process in background `wait` compared to `sleep 3` allows to wait only for the necessary time and because 3s may be not enough, (and may be done outside the loop) – Nahuel Fouilleul Jul 23 '19 at 07:08
  • @Adiii It is a machine learning code. If bash depends upon the return code of the Python, I found the reason, it only allows running Python scripts one by one. So I added `&` in the command and it works now. – GoingMyWay Jul 23 '19 at 09:57
  • this will send them in the background but keep in mind the comment of @shellter – Adiii Jul 23 '19 at 10:06

0 Answers0