Lalith
Lalith

Reputation: 20524

Multiple commands on remote machine using shell script

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?

Upvotes: 28

Views: 64315

Answers (3)

Dirk
Dirk

Reputation: 10111

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 [email protected] bash -c "'
  cd Desktop/testfolder
  java -cp xyz.jar Main
'"

Upvotes: 6

Robin
Robin

Reputation: 4260

Try something like this:

ssh [email protected] "cd /home && ls -l"

Upvotes: 44

Trey Hunner
Trey Hunner

Reputation: 11804

You could try separating the commands by a semicolon:

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

Upvotes: 18

Related Questions