2

I want to run multiple scripts/commands in parallel in bash. Here is the bash script :

python test_script.py &
docker stop process &
docker stop process1 &
sleep(1)
wait

docker start process1 &
docker start process &
wait

Here, im running multiple dockers and want to start/stop docker when my script python test_script.py is running.

for ex: python script is running and on parallel i want to stop process & process1 docker. python script is still in-progress and then say wait or sleep for 1 min and start the process again.

How can i achieve it ?

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
Pooja
  • 481
  • 1
  • 8
  • 15
  • Does this answer your question? [How do you run multiple programs in parallel from a bash script?](https://stackoverflow.com/questions/3004811/how-do-you-run-multiple-programs-in-parallel-from-a-bash-script) – Michael Freidgeim Oct 30 '20 at 12:36

2 Answers2

1

You can combine the following rules:

X; Y Run X and then Y, regardless of success of X

X && Y Run Y if X succeeded

X || Y Run Y if X failed

X & Run X in background.

Alex Dragnea
  • 174
  • 1
  • 6
  • Thanks Alex for answering. I'm looking to run these commands in parallel in script. In the mentioned bash script python test-script is running and then in parallel after some seconds i want to start/stop docker images. – Pooja Nov 17 '17 at 10:07
1

From your original description, it seems that we can organize the problem into two scripts

  • A main python script test_script.py
  • A docker restart script say restart_docker.sh which contains all the docker related commands that will be executed in sequence

    docker stop process
    sleep 1m
    docker start process
    

With GNU parallel, you can provide these two scripts as arguments to be executed in parallel

parallel ::: 'python test_script.py' 'bash restart_docker.sh'
etopylight
  • 1,239
  • 1
  • 10
  • 15
  • Thanks, this helped. But the output of test_script.py is not getting listed in the console / CLI. Is there a way to display the script execution output with parallel command ? – Pooja Nov 19 '17 at 13:04
  • What is the difference between executing the above shared command by you and parallel 'python test_script.py' ::: 'bash restart_docker.sh' – Pooja Nov 19 '17 at 13:10
  • @Pooja Hmm that's odd, `parallel` should forward all the messages generated from each command to stdout, maybe you accidentally added a `&`? – etopylight Nov 19 '17 at 14:05
  • @Pooja For the detail usage of the `parallel` syntax, you can always refer to its manual page. In short, by using `parallel ::: A B`, this means that A and B will run as separate commands while `parallel A ::: B` means that A is a command and B is an argument for A – etopylight Nov 19 '17 at 14:05