The problem you're facing is that Popen() assumes the command to be a list of command+arguments.
There for, cmd1 should look more like:
cmd1 = ['/usr/bin/ssh', '[email protected]', 'time']
(unless dangerously supplying shell=True, then you can keep your command string as is).
You can also use:
import shlex
cmd1 = shlex.split("/usr/bin/ssh [email protected] time")
This is also covered under the docs for Popen().
Last notes: I've never used plpythonu or this SQL version of code execution, but I'd assume that you would want to specify the full path for SSH as Lix pointed out, that being /usr/bin/ssh and not usr/bin/ssh. But your main problem is how you pass the arguments to Popen().
Don't forget to allow SSH to login with a key, otherwise the command might hang asking for a password in the background.
Seeing as many people have issues running system commands that hangs without the programmer understanding why, try the following modification to get output as the command is executed instead of at the end (.communicate() will wait for EOL, meaning hang forever if the process never quits):
CREATE OR REPLACE FUNCTION a()
RETURNS void AS
$BODY$
import subprocess
cmd1 = ["usr/bin/ssh", "[email protected]", "time"]
process = subprocess.Popen(cmd1,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT).communicate()
while process.poll() is None:
plpy.notice(process.stdout.readline())
$BODY$
LANGUAGE plpythonu VOLATILE
It's still highly inefficient since i fused STDERR and STDOUT together and using readline() instead of read(). Also import select might be useful here.
But this will give you output "live" from the command instead of bunkered up at the end. It should also print any errors that the command is throwing (but still hanging because the error didn't close the application properly).
This is a common mistake and people aught to read the examples/docs more on this.
sshcommand should start with the root folder -/usr/bin/ssh. Without the leading slash it would be a relative path - perhaps this is why the "No such file or directory" error is being raised.cmd1should look more like:cmd1 = ['/usr/bin/ssh', '[email protected]', 'time']. This is (if i'm not mistaken) becausePopen()assumes the command parameter is a list unless dangerously supplyingshell=True. You can also usecmd1 = shlex.split("/usr/bin/ssh [email protected] time").plpy.notice()wan't a string? If SSH is hanging waiting for a password,.communicate()will hang and wait as per described in the doc as well:Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. >>>Wait for process to terminate.<<<. Have you tried running this command from the server manually once to see what happens?