Mike
Mike

Reputation: 1844

Creating A Linux at Job (scheduling) in Python

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.

Upvotes: 3

Views: 1989

Answers (2)

zmo
zmo

Reputation: 24812

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)

Upvotes: 2

Blender
Blender

Reputation: 298106

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)

Upvotes: 3

Related Questions