Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple commands on remote machine using shell script

Tags:

java

shell

ssh

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?

like image 713
Lalith Avatar asked Oct 16 '10 18:10

Lalith


People also ask

How do I run a shell command on a remote machine?

Syntax for running commands on a remote Linux or Unix hostUSER-NAME : Remote host user name. REMOTE-HOST : Remote host ip-address or host name, such as fbsd.cyberciti.biz. command or script : Command or shell script is executed on the remote host instead of a login shell.


1 Answers

Try something like this:

ssh [email protected] "cd /home && ls -l" 
like image 164
Robin Avatar answered Oct 11 '22 16:10

Robin