Start job from python, redirect output to file
I have a script, update_file, that I typically run like so:
sudo update_file (file) > ./logs/(file) &
I was wondering what the proper syntax/is it possible, to call this script from within a Python script and still have it redirect output from update_file to a file and have it created as a system job.
EDIT: I should note, I run this against multiple (file)s so I would like to pass that as a variable.
First, the subprocess module is how you execute programs from Python. The section Replacing Older Functions with the subprocess Module in the documentation shows you how to transform typical shell functionality into Python.
Because you're using & to background the task, you'll want to create a Popen, and do the job-handling later. So:
jobs =  # ... each time you want to run it on a file ... jobs.append(subprocess.Popen(['sudo', 'update_file', file], stdout=open(os.path.join('logs', file), 'w')) # ... at exit time ... for job in jobs: job.wait() job.stdout.close()
import subprocess subprocess.call("sudo update_file(file)",stdout=open("logs/(file)","w"))