Reputation: 715
I have 4 python and shell scripts that I would like to run one after another from one python script.
So let's say:
import script1
import script2
import script3
python script1.py &
#do something here to check if script1 has been completed and move to script 2
python script2.py &
#do something here to check if script2 has been completed and move to script 3
python script3.py &
#do something here to check if script3 has been completed and move to script 4 which is a shell script.
Could someone help with this please? thanks in advance!
Upvotes: 1
Views: 2639
Reputation: 1162
Usecase
If you want to add more scripts in future, suggest you create a run.py file that responsible to pass the argument(integer) to the shell file.
Let say you like to run n(4) qty of shell script or python script that accept 1 argument.
Create a file run.python with the code below.
Below code illustrate
To run shell script
# this is python file with name run.py
import subprocess,os
instanceQty = 4
for i in range(0, instanceQty):
print(os.getcwd())
subprocess.Popen(f"{os.getcwd()}/mockScript.sh {i}",shell=True,executable='/bin/bash')
To run python script
import subprocess,os,sys
instanceQty = 4
for i in range(0, instanceQty):
print(os.getcwd())
subprocess.Popen([sys.executable,f"{os.getcwd()}/mockScript.py",str(i)])
Run this file with
python run.py
permission problem on MacOS
sudo chmod ug+x mockScript.sh
sudo chmod ug+x run.py
All code tested on Python 3.8.1 and MacOs 12.0.1 environment.
Upvotes: 0
Reputation: 207425
I would do this:
#!/usr/bin/env python
import subprocess
subprocess.run(['python', 'script1.py'])
subprocess.run(['python', 'script2.py'])
subprocess.run(['python', 'script3.py'])
If you only want each script to run if the previous one was successful:
#!/usr/bin/env python
import subprocess
subprocess.run('python script1.py && python script2.py && python script3.py', shell=True)
I am using shell=True
here because I am relying on the shell to interpret the &&
and only let the next process run if the previous one was successful.
If you want them all to run in parallel with each other, and in the background:
#!/usr/bin/env python
import subprocess
subprocess.run('python script1.py &', shell=True)
subprocess.run('python script2.py &', shell=True)
subprocess.run('python script3.py &', shell=True)
I am using shell=True
here because I am relying on the shell to interpret the &
to mean that I want the processes to run run in the background so that I can carry on doing something else while they run.
In general, I wouldn't use Python at all for this, I would write a bash
script like this:
#!/bin/bash
python script1.py
python script2.py
python script3.py
Also, in general, I would make the first line of a Python script a shebang like this:
#!/usr/bin/env python
print('I am a Python script with shebang')
then I would make the script executable with:
chmod +x script.py
Now, instead of running it with:
python script.py
the kernel knows which interpreter to use so I don't have to tell it every time and I can simply run it with:
script.py
if the directory it is located in is on my PATH. Or, if it is not on my PATH, I'd need:
/path/to/script.py
Upvotes: 2