2

I have the following code :

def executeRemoteCommand(host, command):
    cmd = "ssh " + host + " \'" + command + "\'"
    print cmd
    subprocess.check_call(cmd)

When I run the command:

java -Xss515m -Xms48g -Xmx48g -XX:+UseConcMarkSweepGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -jar /local/experiments/helloworld/ro/server.jar /local/experiments/helloworld/ro/properties.json

using the function above, I get the following error

File "./util/ssh_util.py", line 85, in executeRemoteCommand
    subprocess.check_call(cmd)
  File "/usr/lib/python2.7/subprocess.py", line 506, in check_call
    retcode = call(*popenargs, **kwargs)
  File "/usr/lib/python2.7/subprocess.py", line 493, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1259, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory.

However, when I type in the call directly in the command line, it works fine.

ssh foo92 'java -Xss515m -Xms48g -Xmx48g -XX:+UseConcMarkSweepGC -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -jar /local/experiments/helloworld/ro/server.jar /local/experiments/helloworld/ro/properties.json'

Does anyone have any idea?

1 Answer 1

6

You need to set shell=True to execute that command through a shell:

subprocess.check_call(cmd, shell=True)

Rather than push this through a shell, you can execute it directly if you pass in the arguments as a list; that way you don't have to worry about quoting the command either:

def executeRemoteCommand(host, command):
    subprocess.check_call(['ssh', host, command])

Note that both host and command here are single arguments passed to ssh. Normally, that is how the shell would pass the arguments into the ssh command, that's what the quoting around the java ... command line is for.

Sign up to request clarification or add additional context in comments.

3 Comments

But it may be significant to remember that -jar /local/experiments/helloworld/ro/server.jar is a single argument.
@SteveBarnes: not just -jar ..., the whole java ... command is a single argument to the ssh command.
@SteveBarnes: if you were to invoke java directly, then -jar and /local/experiments/helloworld/ro/server.jar would be two command line arguments; that is how the shell would pass it in as there is a space between the two strings. It is the java executable itself that then parses these arguments and pulls in the next string if there is a -jar string.

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.