Kick off a delete command from python and not wait

L

loial

I have a requirement to kick off a shell script from a python script
without waiting for it to complete. I am not bothered about any return
code from the script.

What is the easiest way to do this. I have looked at popen but cannot
see how to do it.
 
C

Chris Rebert

I have a requirement to kick off a shell script from a python script
without waiting for it to complete. I am not bothered about any return
code from the script.

What is the easiest way to do this. I have looked at popen but cannot
see how to do it.

Use the `subprocess` module.

import subprocess
proc = subprocess.Popen(["shell_script.sh", "arg1", "arg2"],
stdout=subprocess.PIPE, stderr=subprcoess.PIPE)
# lots of code here doing other stuff
proc.wait()

I believe you need to /eventually/ call .wait() as shown to avoid the
child becoming a zombie process.

Cheers,
Chris
 
N

Nobody

I believe you need to /eventually/ call .wait() as shown to avoid the
child becoming a zombie process.

Alternatively, you can call .poll() periodically. This is similar to
..wait() insofar as it will "reap" the process if it has terminated, but
unlike .wait(), .poll() won't block if the process is still running.

On Unix, you can use os.fork(), have the child execute the command in the
background, and have the parent wait for the child with os.wait(). The
child will terminate as soon as it has spawned the grandchild, and the
grandchild will be reaped automatically upon termination (so you can
forget about it).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top