3

I'm trying to schedule a number of jobs in linux using a python script. Currently, my script looks something like this:

import subprocess
command = 'python foo.py %s %s | at %s' % (arg1, arg2, starttime) 
subprocess.Popen([command,], shell=True)

But this doesn't appear to work and I was hoping someone might be able to advise on what I should be doing.

1
  • Popen can run a single command, but what you have is an entire pipeline. If foo.py is in python, why don't you just import it, though?
    – tripleee
    May 20, 2012 at 19:10

2 Answers 2

3

If you pass in shell=True, Popen() expects a string:

import subprocess
command = 'python foo.py %s %s | at %s' % (arg1, arg2, starttime) 
subprocess.Popen(command, shell=True)

Also, what do you mean by "isn't working"? It works fine for me:

>>> import subprocess
>>> subprocess.Popen('echo "asd" | rev', shell=True).communicate()
dsa
(None, None)

And your code as well:

>>> import subprocess
>>> subprocess.Popen(['echo "asd" | rev',], shell=True).communicate()
dsa
(None, None)
2

what is your problem is the use of the at command, it shall get a string, so what you want is more

command = 'echo python foo.py %s %s | at %s' % (arg1, arg2, starttime)

or in a more pythonic way

sched_cmd = ['at', starttime]
command = 'python foo.py %s %s' % (arg1, arg2)
p = subprocess.Popen(sched_cmd, stdin=subprocess.PIPE)
p.communicate(command)
1
  • I always tend to avoid using shell=True, as you can't manage your inputs and the behaviour correctly.
    – zmo
    May 20, 2012 at 19:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.