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? Commented 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)
Sign up to request clarification or add additional context in comments.

Comments

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 Comment

I always tend to avoid using shell=True, as you can't manage your inputs and the behaviour correctly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.