Keerthimanu
Keerthimanu

Reputation: 51

How to cd to a directory on remote machine and run the script present in that directory

When I do this

ssh host@someip "cd /target && ls"

the files on remote machine are displayed.

Now, I am trying to run a file on the remote machine.

ssh host@someip "cd /target && ./file.sh"

When i do this it reaches the folder named target on the remote machine but the file is executed on local machine. How to make file.sh also run on remote machine ?

Upvotes: 0

Views: 1701

Answers (1)

Jakuje
Jakuje

Reputation: 25956

It depends where do you initialize your libraries on the remote server. There are several possibilities, which can be wrong, but without exact error, it is impossible to guess:

  1. Your ~/.bashrc (or other startup files) do not load libraries for non-interactive jobs. You can force TTY allocation:

    ssh -t host@someip "cd /target && ./file.sh"
    
  2. You might initialize libraries with in some other script. You can load it before running your script:

    ssh host@someip "source your_init_file && cd /target && ./file.sh"
    

Upvotes: 2

Related Questions