2

I want to run multiple Python scripts from shell. I used pythonDim.sh script to do the same.

#!/usr/bin/python
/home/path_to_script/dimAO.py
/home/path_to_script/dimA1.py
/home/path_to_script/dimA2.py
/home/path_to_script/dimA3.py

But it's not working. How to write the shell script?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Samradnyee
  • 49
  • 1
  • 1
  • 7
  • 1
    That shebang is wrong; although the script may *run* Python scripts, it can't itself be executed as Python. – jonrsharpe Mar 05 '18 at 08:03
  • 2
    use `#!/usr/bin/env bash` to run it via a `bash` shell https://stackoverflow.com/questions/10376206/what-is-the-preferred-bash-shebang or any other shebang depending on the shell you want to use – Allan Mar 05 '18 at 08:04
  • 2
    As mentioned above - change the header to bash and start each line by calling python, for example: `python /home/path_to_script/dimAO.py` – Nir Alfasi Mar 05 '18 at 08:07
  • *I want to run multiple Python scripts from shell* - but your `#!` line says *python*, not a shell. – cdarke Mar 05 '18 at 08:11
  • is it necessary to add header in python script `dimA0.py`. and is it necessary to make scripts executable? – Samradnyee Mar 05 '18 at 08:18

1 Answers1

7

To run sequentially:

#!/bin/bash
/home/path_to_script/dimAO.py
/home/path_to_script/dimA1.py
/home/path_to_script/dimA2.py
/home/path_to_script/dimA3.py

To run them all in parallel:

#!/bin/bash
/home/path_to_script/dimAO.py &
/home/path_to_script/dimA1.py &
/home/path_to_script/dimA2.py &
/home/path_to_script/dimA3.py &

Use redirection (> or >>) to redirect stdout and stderr, as desired.

Sharad
  • 9,282
  • 3
  • 19
  • 36
  • 1
    he should call every python script using `python ` and to run in // I would recommend to add `nohup` in order to avoid that by closing your shell it kills all the processes!!!! – Allan Mar 05 '18 at 08:54