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?
Just in case.. I also got stuck with this error and the issue was that my files were in DOS instead of UNIX so at :
where lst_exp is a list of args, one of them was "not found" because it was in DOS instead of UNIX but error thrown was the same :
I got same error when i wrote like this :-
And problem is solved when i made shell=True .It will work
subprocess.Popen("ls" ,shell = False , stdout = subprocess.PIPE ,stderr = subprocess.PIPE, shell=True)
Use
["screen", "-dmS", "RealmD", "top"]
instead of["screen -dmS RealmD top"]
.Maybe also use the complete path to
screen
.If the program still cannot be found you can also go through your shell with
shell=True
, but then you need to quote and escape your parameters etc. Make sure to read the information in the docs if you plan to do that.The problem is that your command should be split. subprocces requires that the cmd is a list, not a string. It shouldn't be:
That won't work. If you feed subprocess a string, it assumes that that is the path to the command you want to execute. The command needs to be a list. Check out http://www.gossamer-threads.com/lists/python/python/724330. Also, because you're using file redirection, you should use
subprocess.call(cmd, shell=True)
. You can also useshlex
.Only guess is that it can't find
screen
. Try/usr/bin/screen
or whateverwhich screen
gives you.