Source a script remotly via ssh

限于喜欢 提交于 2020-01-24 10:46:57

问题


I want to run a remote program via ssh which requires a certain environment. Thus before executing the program I source a specific file building up the environment. If I'm logged onto the machine directly this is no problem but when I execute the command via ssh

#!/bin/bash
foo=`ssh user@host "source ~/script.sh; ~/run/program"`

I get an error that indicates that the script was not sourced correctly. Do you know what I have to do in order to get the script sourced and the program executed in the same session?

EDIT: I'm exporting the LD_LIBRARY_PATH with the script and the executable is complaining that it cannot find the shared object file. The default shell is bash. 'Session' is definitive not the right wording. I meant 'terminal environment'.


回答1:


This may not be the cleanest way, but if you invoke bash with the interactive option (-i) and send commands through the standard input, it should work.

In particular,

foo=`ssh user@host bash -i <<EOF
source ~/script.sh
~/run/program
EOF`

It would be much easier if you have a script program_in_env.sh that does exactly the two steps you want:

#!/bin/bash
source ~/script.sh
~/run/program

Then you would just need to call ssh user@host program_in_env.sh.

Good luck.




回答2:


Thank you for all your time and help. I found the issue. The basic idea of how to execute the remote program was right from the beginning. When testing my case locally on the machine, the current working directory was different. For some reason the cwd is important when sourcing this bash script.



来源:https://stackoverflow.com/questions/25870899/source-a-script-remotly-via-ssh

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!