views:

2261

answers:

2

I have the following code that is attempting to start each of the "commands" below in Linux. The module attempts to keep each of the 2 commands running if either should crash for whatever reason.

#!/usr/bin/env python
import subprocess

commands = [ ["screen -dmS RealmD top"], ["screen -DmS RealmD top -d 5"] ]
programs = [ subprocess.Popen(c) for c in commands ]
while True:
    for i in range(len(programs)):
        if programs[i].returncode is None:
            continue # still running
        else:
            # restart this one
            programs[i]= subprocess.Popen(commands[i])
        time.sleep(1.0)

Upon executing the code the following exception is thrown:

Traceback (most recent call last):
  File "./marp.py", line 82, in <module>
    programs = [ subprocess.Popen(c) for c in commands ]
  File "/usr/lib/python2.6/subprocess.py", line 595, in __init__
    errread, errwrite)
  File "/usr/lib/python2.6/subprocess.py", line 1092, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

I think I'm missing something obvious, can anyone see what's wrong with the code above?

+2  A: 

Only guess is that it can't find screen. Try /usr/bin/screen or whatever which screen gives you.

Colin Burnett
+5  A: 

Use ["screen", "-dmS", "RealmD", "top"] instead of ["screen -dmS RealmD top"].

Maybe also use the complete path to screen or pass shell=True to Popen if the program still cannot be found, but this probably isn't necessary.

sth
Docs say a string or a sequence can be used.
Colin Burnett
commands = [ ["screen", "-dmS", "RealmD", "top"], ["screen", "-DmS", "RealmD", "top", "-d", "5"] ]Worked perfectly!
Caedis
@colin: In this case a sequence was used and it needs to contain the parameters separately. Probably a simple string (without the []) would also work.
sth
Either 1 string, or 1 parameter "word" per element of a list. If you pass a sequence, you should have done the exact parsing that a shell normally does. If you pass a single string it's parsed exactly the way the shell would have parsed it.
S.Lott