process

Command

https://docs.python.org/3.4/library/subprocess.html#subprocess.call

call is a convenience function.

Run command with arguments. Wait for the command to complete:

import subprocess

result = subprocess.call([
    'psql',
    '-X',
    '-U',
    'postgres',
    '-c',
    "DROP DATABASE test_api"
])
if result < 0:
    raise "Child was terminated by signal " + str(result)

My original notes had this example:

result = subprocess.call("svn move " + txtFile + " " + aptFile, shell=True)

Warning

Not sure why we would pass in the whole command string, or use shell=True - so check before using.

Background

To run a task in the background:

process = None
try:
    process = subprocess.Popen(['uwsgi', 'uwsgi-api.ini'])
    # do some stuff
finally:
    if process:
        process.kill()

Environment

To set the environment for the process:

import os
os.putenv('JAVA_HOME', './bin/java/jdk1.6')
# continue as above...

Folder

To run the command in a specific folder, use the cwd parameter:

result = subprocess.call(command, cwd=folder, shell=True)

Sleep

See time.