Bash – Execute code on remote machine and copy the results back

bashremoteshellssh

I'm using some old Fortran code that uses some peculiar memory handling. To make the long story short, it runs on my local machine but fails on the remote one. This is why I would like to ssh to my local computer run the code and copy the results back to the cluster I'm running my calculations on.

I already found exactly the same question on this forum:

EDIT #1

After the comment by @Anthon, I corrected my script, unfortunately new error occurred. NOTE: I am using ssh keys so no passwords are needed.

My new script:

#! /bin/bash
# start form the machine_where_the_resutlst_are_needed

ssh usr@machene_wehere_i_run_the_code /home/run_dir_script/run.sh inp 8

# do the work by running a script. 8 jobs are run by sending them 
# to the background, 

scp -p usr@machene_wehere_i_run_the_code:/home/run_dir_script/results \
  user@machine_where_the_resutlst_are_needed:~/

echo "I am back"

My problem is that run.sh is a master script calling other shell scripts, and they don't run properly. I get the following message:

/home/run_dir_script/run.sh: line 59: /home/run_dir_script/merge_tabs.sh: No such file or directory

Minimal Example:

Here is a condensed example of what I am doing

Example run.sh

#! /usr/bin/bash

pwd
echo "Run the code"
./HELLO_WORLD

The above script is run by

ssh usr@machene_wehere_i_run_the_code /home/run_dir_script/run.sh    

For completeness the fortran code ./HELLO_WORLD

program main
write(*,*) 'Hello World'
stop
end

Compile with
gfortran -o HELLO_WORLD hello_world.F90

And here is the output

/home/run_dir_script/
Run the code
/home/run_dir_script/test.sh: line 5: ./home/HELLO_WORLD: No such file or directory

Remark:

The following will run `HELLO_WORLD` on the remote machine
ssh usr@machene_wehere_i_run_the_code /home/run_dir_script/HELLO_WORLD

So calling the code directly works fine. Calling it via the script fails.

Possible Solution:

The reason why this fails is due to the fact that after ssh I land in my remote machine's $HOME.

Therefore before executing the script, I have to cd in the proper directory. The correct method, besides giving absolute path is:

Another useful remark, is that I all the variables from .bashrc are undefined. Therefore one has to be careful.

 usr@machene_wehere_i_run_the_code "cd /home/run_dir_script ; run.sh"

So this somehow works

Best Answer

There is nothing after on the line after ssh -X usr@machene_wehere_i_run_the_code in your code. So that command logs in on machene_wehere_i_run_the_code and does nothing.

In the example ssh call in the accepted answer of the question you quote there is an extra parameter:

ssh user@host path_to_script

and the path_to_script is missing in yours.

Related Question