1

code-1 : Passing linux commands as a sequence of arguments

from subprocess import Popen, PIPE

run_cmd = Popen(['ls','-l','mkdir','hello'], stdout = PIPE, stderr = PIPE)
output,error = run_cmd.communicate()
print error,output, run_cmd.returncode

Output - 1:

ls: cannot access mkdir: No such file or directory
ls: cannot access hello: No such file or directory
 2

In the above code I am trying to run multiple linux commands by passing them as a sequence of arguments. If I am modifying the above code to the following one it works fine.

code-2 : Pass linux commands as a string

from subprocess import Popen, PIPE

run_cmd = Popen('mkdir hello; ls -l; echo Hello; rm -r hello', shell=True, stdout = PIPE, stderr = PIPE)
output,error = run_cmd.communicate()
print error,output, run_cmd.returncode

Output - 2 :

drwxrwxr-x. 2 sujatap sujatap    6 May  9 21:28 hello
-rw-rw-r--. 1 sujatap sujatap   53 May  8 20:51 test.py
Hello
0

As shell=True is not a suggested way to be used so I want to run the linux commands using the former one. Thanks.

1 Answer 1

3

If something does not work, check its documentation: https://docs.python.org/2/library/subprocess.html#popen-constructor

args should be a sequence of program arguments or else a single string. By default, the program to execute is the first item in args if args is a sequence. If args is a string, the interpretation is platform-dependent and described below. See the shell and executable arguments for additional differences from the default behavior. Unless otherwise stated, it is recommended to pass args as a sequence.

So test your single program runs first (list of program and its arguments), then make a list of lists and run them in sequence with a loop:

myprogramsequence = [
    ["ls", "-l"],
    ["mkdir", "hello"]
]

for argumentlist in myprogramsequence:
    run_cmd = Popen( argumentlist, ...
Sign up to request clarification or add additional context in comments.

7 Comments

Note the wording: it does not have to be a list, but could be any sequence type.
Thanks for the approach. If I try this method multiple commands are run parallely. Yeah I got the point of sequence.
That's because it does not wait for the new process to finish. Look if one of the subprocess convenience functions does what you need (same link, a bit further up on the page).
The underlying of the convenience functions uses Popen. So I will go with that.
If you want to use Popen directly, Popen.wait() is for you.
|

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.