Run python script after another running script ends
Question:
I have a python script running that will take a long time. I want to run another script immediately after the first finishes. It’s of no use to make a bash program to sequentially run the scripts because the first is already running.
How do I run some Python script after the one that is already running finishes?
PS the script that is running is running on a screen
, and there could be more than one screen
open.
Answers:
You could import the code from the second script, and run it at the end of the first one.
The first thing I thought of is running both Python programs from within a shell script so the 2nd one simply starts when the first one ends:
#!/bin/bash
python -m script1.py
python -m script2.py
If the first script is already running, or started outside of your control, you could use the ps command in a loop to detect when it finishes, and then launch the second the 2nd script:
#!/bin/bash
while [ 1 ]
do
cnt=`ps x | grep -v grep | grep -c script1.py`
if [[ "$cnt" -eq 0 ]]
then
break
fi
sleep 1
done
echo "Starting second program"
python -m script2.py
Tip: when using this approach, remember to use grep -v grep
before the grep that looks for the process you want, or your grep command will count itself in the results.
(Please be gentle; this is my first attempt at answering a question here.)
I have a python script running that will take a long time. I want to run another script immediately after the first finishes. It’s of no use to make a bash program to sequentially run the scripts because the first is already running.
How do I run some Python script after the one that is already running finishes?
PS the script that is running is running on a screen
, and there could be more than one screen
open.
You could import the code from the second script, and run it at the end of the first one.
The first thing I thought of is running both Python programs from within a shell script so the 2nd one simply starts when the first one ends:
#!/bin/bash
python -m script1.py
python -m script2.py
If the first script is already running, or started outside of your control, you could use the ps command in a loop to detect when it finishes, and then launch the second the 2nd script:
#!/bin/bash
while [ 1 ]
do
cnt=`ps x | grep -v grep | grep -c script1.py`
if [[ "$cnt" -eq 0 ]]
then
break
fi
sleep 1
done
echo "Starting second program"
python -m script2.py
Tip: when using this approach, remember to use grep -v grep
before the grep that looks for the process you want, or your grep command will count itself in the results.
(Please be gentle; this is my first attempt at answering a question here.)