user3516540 user3516540 - 1 month ago 15
Linux Question

Error running shell command with subprocess.check_call()

# -*- coding: utf-8 -*-
import rpyc
conn = rpyc.classic.connect(r"xx.xx.xx.xx")
s = conn.modules.subprocess.check_output(['file',u'`which dd`'])
print s


Output is:

`which dd`: cannot open ``which dd`' (No such file or directory)

Process finished with exit code 0


When I execute manually on the command prompt it gives me proper output :

/bin/dd: symbolic link to /bin/dd.coreutils


Is there any Unicode error in my code

Answer

It runs on the command prompt. But if you will call it through the subprocess (so will be with conn.modules.subprocess), it will give you error as:

>>> subprocess.check_call(['file', '`which python`'])
`which python`: cannot open ``which python`' (No such file or directory)

Because in shell, this will be executed as:

 mquadri$ file '`which python`'
`which python`: cannot open ``which python`' (No such file or directory)

But you want to run it as:

mquadri$ file `which python`
/usr/bin/python: Mach-O universal binary with 2 architectures
/usr/bin/python (for architecture i386):    Mach-O executable i386
/usr/bin/python (for architecture x86_64):  Mach-O 64-bit executable x86_64

In order to make the above command run, pass it as string to check_call with shell=True as:

>>> subprocess.check_call('file `which python`', shell=True)
/usr/bin/python: Mach-O universal binary with 2 architectures
/usr/bin/python (for architecture i386):    Mach-O executable i386
/usr/bin/python (for architecture x86_64):  Mach-O 64-bit executable x86_64