-2

i have this command which runs on the prompt:

echo "python setHeater.py" | at 16:30

how can i execute that from a Python program?

In my program i create a date, and concat that to a string, something like this

newtime = createnewtime()
commandToExecute = 'echo "python setHeater.py" | at ' + newtime 
#and then here the code to actually run the command in the command environment
Michel
  • 23,085
  • 46
  • 152
  • 242
  • paste your entire python script? Are you using datetime to make your time (with a custom function of createnewtime())? Also, what's the end goal? Really just to print this to stdout? Are you trying to actually "echo" this via sys/os modules? – Justin Carroll Mar 25 '13 at 15:35
  • Why run a python script from a python script? If you really want to execute that command then see http://stackoverflow.com/questions/89228/calling-an-external-command-in-python. But I'd recommend importing what's in setHeater.py into your current script and do the scheduling in Python. Otherwise you have unnecessary overhead. – Jon Cairns Mar 25 '13 at 15:38
  • @Jonathan i am not trying to run a python script from my python script, i am trying to schedule the next run for my script – Michel Mar 25 '13 at 15:41
  • @Nascent: i'm running a script (can't post it yet, it's full of loose ends like this :-)) and in that script, i will schedule the next run time for that script (using the `at` program) – Michel Mar 25 '13 at 15:43

2 Answers2

1

Basically you could execute a command using subprocess library like:

from subprocess import Popen, PIPE

newtime = createnewtime()
p1 = Popen(["echo ", "'python setHeater.py'"], stdout=PIPE)
p2 = Popen(["at", newtime ], stdin=p1.stdout, stdout=PIPE)
output = p2.communicate()[0]
huzeyfe
  • 3,554
  • 6
  • 39
  • 49
  • 1
    Instead of running setHeater.py explicitly, use `import sys` and then `sys.argv[0]` - this means the script will still work if you change the name. – Jon Cairns Mar 25 '13 at 15:52
  • When i run this, i get this result: – Michel Mar 25 '13 at 16:02
  • `/usr/bin/python /home/setHeater.py echo "/usr/bin/python /home/setHeater.py" | at 17:01 Traceback (most recent call last): File "/home/setHeater.py", line 6, in returnCode = call(commandToExecute) File "/usr/lib/python2.7/subprocess.py", line 493, in call return Popen(*popenargs, **kwargs).wait() File "/usr/lib/python2.7/subprocess.py", line 679, in __init__ errread, errwrite) File "/usr/lib/python2.7/subprocess.py", line 1259, in _execute_child raise child_exception OSError: [Errno 2] No such file or directory` – Michel Mar 25 '13 at 16:02
  • Source is then: `from subprocess import call` `newtime = "17:01"` `commandToExecute = 'echo "/usr/bin/python /home/setHeater.py" | at ' + newtime` `print(commandToExecute)` `returnCode = call(commandToExecute)` `print(returnCode)` – Michel Mar 25 '13 at 16:03
  • Hm... can't seem to get the formatting in a comment, i use backticks around every line... – Michel Mar 25 '13 at 16:06
  • When you changed source did it work? – huzeyfe Mar 25 '13 at 16:18
  • when i use your example but change the commandToExecute line to `+ '17.30'` at the end (so hardcode the time) it gives me the same errors. Reading the errors, I'm not sure which file or direcory it can not find? – Michel Mar 25 '13 at 16:25
  • When i change the commandtoExecute to `echo "Michel" > michel.txt` i get the same error.... Can i use the `|` and `>` symbols in the `call` command? – Michel Mar 25 '13 at 16:28
  • what if you try this: commandToExecute = "python setHeater.py | at " + newtime? – huzeyfe Mar 25 '13 at 16:28
  • Same errors, can the PIPE symbol do any damage? – Michel Mar 25 '13 at 16:31
  • Yep possible I've edited code please try that. – huzeyfe Mar 25 '13 at 16:38
  • Still no luck. I've replaced the newtime with a hardcoded string `File "setHeater.py", line 4, in p1 = Popen(["echo ", "'python setHeater.py'"], stdout=PIPE) File "/usr/lib/python2.7/subprocess.py", line 679, in __init__ errread, errwrite) File "/usr/lib/python2.7/subprocess.py", line 1259, in _execute_child raise child_exception OSError: [Errno 2] No such file or directory` – Michel Mar 25 '13 at 17:44
1

You can use the OS library:

import os

newtime = createnewtime()
command = 'echo "python setHeater.py" | at ' + newtime
os.system(command)

Although if you are trying to execute this command you don't need to use "echo". Simply:

import os

newtime = createnewtime()
command = "python setHeater.py | at " + newtime
os.system(command)
Andy
  • 26
  • 1