27

I have a Java program Desktop/testfolder/xyz.jar on a remote machine. It has a configuration file on the same folder. When I SSH into the machine, I do:

"ssh user@remote java -cp Desktop/testfolder/xyz.jar Main"

The problem here is the configuration file is not in the path, as we are in the home folder so my program cannot read the configuration.

I want to first go into that folder and then run the program from that folder. In a shell script if I did this

"ssh user@remote cd Desktop/testfolder"
"java -cp xyz.jar Main"

it executes the first statement and when the second statement is run it runs on my current machine not the remote machine.

Can we do only one command or there are any other solutions for this?

jww
  • 83,594
  • 69
  • 338
  • 732
Lalith
  • 17,686
  • 14
  • 42
  • 54
  • Possible duplicate of [What is the cleanest way to ssh and run multiple commands in Bash?](https://stackoverflow.com/q/4412238/608639) – jww Aug 23 '19 at 20:42

3 Answers3

43

Try something like this:

ssh you@yours.com "cd /home && ls -l"
Devin Burke
  • 13,074
  • 11
  • 51
  • 80
Robin
  • 4,096
  • 15
  • 20
  • Thanks both of your comments helped ! – Lalith Oct 16 '10 at 19:06
  • 5
    You're welcome! Trey's and my answer do the same thing provided the first part of the command "works" - the version with the && doesn't perform the second part of the command if the first part fails. For example, compare `ssh you@yours.com "cd /non-existant ; ls -l"` to `ssh you@yours.com "cd /non-existant && ls -l"`. In the first case you still get some output from the `ls -l` command (even though the `cd` failed - probably the home dir of the `you` user) and in the latter case the `ls -l` command is skipped because the first *operand* returned *false*. – Robin Oct 16 '10 at 21:03
18

You could try separating the commands by a semicolon:

ssh user@remote "cd Desktop/testfolder ; java -cp xyz.jar Main"
Trey Hunner
  • 8,794
  • 4
  • 42
  • 82
  • 32
    It is important to note the difference between these two operators: **&&** and **;**. **&&** Means that the second command will execute *if, and only if* the first command executes and returns with an exit status of zero (no errors) while **;** allows the following command to execute regardless of it's exit status. – earthmeLon Jun 23 '12 at 05:36
  • It is also worth noting that a newline is permitted in the string where you have a semicolon. (Yes, [regular strings can span multiple lines in `sh`](http://stackoverflow.com/a/9140181/874188) and derivatives, without any additional syntax at all.) – tripleee Dec 18 '15 at 16:11
  • ...to be a little more explicit about the relevance of the prior comment here -- using `;` instead of `&&` means that the `java` interpreter can be invoked in the wrong directory if the `cd` fails, whereas `;` will prevent that. Using `&&` is thus more correct. – Charles Duffy Dec 26 '18 at 14:58
6

If you want to split your commands over multiple lines for the sake of readability, you could also pass the list of commands to the bash command as follows:

ssh user@remote.host bash -c "'
  cd Desktop/testfolder
  java -cp xyz.jar Main
'"
Dirk
  • 7,235
  • 15
  • 56
  • 86