在遠端機器上執行程式碼並將結果複製回來

在遠端機器上執行程式碼並將結果複製回來

我正在使用一些舊的 Fortran 程式碼,這些程式碼使用了一些特殊的記憶體處理。長話短說,它在我的本地電腦上運行,但在遠端電腦上失敗。這就是為什麼我想ssh在本地計算機上運行程式碼並將結果複製回我正在運行計算的叢集。

我已經在這個論壇上發現了完全相同的問題:

編輯#1

在@Anthon發表評論後,我更正了我的腳本,不幸的是出現了新的錯誤。筆記:我使用的是 ssh 金鑰,因此不需要密碼。

我的新腳本:
#! /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"

我的問題是run.sh主腳本呼叫其他 shell 腳本,而且它們無法正常運作。我收到以下訊息:

/home/run_dir_script/run.sh:第 59 行:/home/run_dir_script/merge_tabs.sh:沒有這樣的檔案或目錄

最小範例:

這是我正在做的事情的一個濃縮範例

例子run.sh

#! /usr/bin/bash

pwd
echo "Run the code"
./HELLO_WORLD

上面的腳本是由

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

為了完整起見,fortran 碼 ./HELLO_WORLD

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

使用 gfortran -o HELLO_WORLD hello_world.F90 編譯

這是輸出

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

評論:

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

所以直接呼叫程式碼就可以了。透過腳本呼叫它失敗。

可能的解決方案:

失敗的原因是在 ssh 之後我登陸了遠端機器的$HOME.

因此,在執行腳本之前,我必須cd進入正確的目錄。除了給出絕對路徑之外,正確的方法是:

另一個有用的註解是,.bashrc 中的所有變數都未定義。因此,人們必須小心。

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

所以這在某種程度上有效

答案1

我會嘗試將參數放在ssh雙引號中。

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

另外,根據該錯誤訊息,聽起來腳本找不到此腳本:

/home/run_dir_script/run.sh:第 59 行:/home/run_dir_script/merge_tabs.sh:沒有這樣的檔案或目錄

scp如果ssh沒有返回成功狀態,我也會阻止發生:

ssh usr@machene_wehere_i_run_the_code "/home/run_dir_script/run.sh inp 8"
status=$?

if $status; then
  scp -p usr@machene_wehere_i_run_the_code:/home/run_dir_script/results \
    user@machine_where_the_resutlst_are_needed:~/
fi

但底線問題是您的腳本在遠端系統上定位從屬腳本時存在問題。當您登入並執行腳本時,與透過登入ssh並執行腳本時相比,可能會設定一些變數。

env對於這些,我將比較使用兩種方法的輸出。

答案2

ssh -X usr@machene_wehere_i_run_the_code您的程式碼中的after 行中沒有任何內容。因此該命令登入後machene_wehere_i_run_the_code不執行任何操作。

在您引用的問題的接受答案中的 ssh 呼叫範例中,有一個額外的參數:

ssh user@host path_to_script

path_to_script你的卻不見了。

相關內容