Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python execute shell command and continue without waiting and check if running before executing

I need to execute two other python scripts from another one. commands look like this:

# python send.py

# python wait.py

This will happen in a loop that will sleep for 1 minute then re-run.

Before executing the commands to start the other scripts I need to make sure they are not still running.

like image 875
transilvlad Avatar asked Dec 01 '22 22:12

transilvlad


1 Answers

You can use subprocess.Popen to do that, example:

import subprocess

command1 = subprocess.Popen(['command1', 'args1', 'arg2'])
command2 = subprocess.Popen(['command2', 'args1', 'arg2'])

if you need to retrieve the output do the following:

command1.wait()
print command1.stdout

Example run:

sleep = subprocess.Popen(['sleep', '60'])
sleep.wait()
print sleep.stdout  # sleep outputs nothing but...
print sleep.returncode  # you get the exit value
like image 167
amirouche Avatar answered Dec 04 '22 05:12

amirouche