1

update:

Sorry it seems the command is executed on remote but just not in the path of where script.sh exists.

So I modified the question title from

How do I run a executable .sh file on a remote server from local machine?

to

How do I run a executable .sh file on a remote server from local in its remote-existed path?


I created a shell script my_script.sh on my remote server "111.111.111.111". It works with the server's file system and do some job that I want.

Then I want to run the command from local terminal like:

ssh -p 22 [email protected] "~/user/path/my_script.sh"

But I noticed that the shell is executed at the ~/user/ path not the ~/user/path/.

What should I do to make sure the shell script performed exactly as I type my_script.sh in the remote terminal in this case?

Thanks

1 Answer 1

5

ssh [email protected] 'cd ~/user/path && ./my_script.sh'

2
  • thanks, but what is the difference between your answer and cd ~/user/path && ./my_script.sh? All the same? Commented Sep 2, 2015 at 16:04
  • The difference between ; and && as command separators is that if you use &&, the whole command line will abort if the first one fails. That's probably better, in this case; you might have a my_script.sh in the home directory, which could be run by accident. Edited.
    – Tom Hunt
    Commented Sep 2, 2015 at 16:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .