3

I have a script that will run queries in Hive automatically. I can either leave the script on my local machine or put it on the gateway machine. If I have it on the remote machine, I can run update_table like

ssh [email protected] ‘./update_table.sh'

However, when I do this, it doesn't recognize the environment variables that are set on the remote machine. If I logged into the remote machine via ssh, and just ran ./update_table.sh it runs fine. How do I get it to recognize the environment variables when running from my local machine?

4 Answers 4

2

When a command is specified with ssh, the ssh won't allocate pseudo terminal (tty) for the session by default. As a consequence a different set of startup scripts is (might be) sourced. And/or different branches in the scripts are taken, based on absence/presence of TERM environment variable.

See ssh man:

When the user's identity has been accepted by the server, the server either executes the given command in a non-interactive session or, if no command has been specified, logs into the machine and gives the user a normal shell as an interactive session.


To force ssh to allocate tty nevertheless, use -t switch:

ssh -t [email protected] ‘./update_table.sh'     

See ssh man:

-t Force pseudo-terminal allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g. when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.


Though rather than working around the issue by forcing allocation of the pseudo terminal, you should better fix your startup scripts to set the same environment for all sessions.

Sign up to request clarification or add additional context in comments.

Comments

0

Maybe you can source your env by using:

ssh [email protected] 'source /etc/bashrc && source /etc/profile && ./update_table.sh'

or:

ssh [email protected] 'source ~/.bashrc && source ~/.bash_profile && ./update_table.sh'

2 Comments

How would i specify to look on the remote machine rather than the local machine?
just edit your update_table.sh and add source /etc/bashrc/ or source /etc/profile. it will source your remote env.
0

What about sourcing your environment variable from your bash_rc?

ssh [email protected] "source ~/.bashrc; ./update_table.sh"

1 Comment

Sorry, will this source the bashrc on the remote machine?
-1

Try with Jenkins pipeline. It will work. example:- sh "ssh (then your ssh lines)"

3 Comments

A little more explanation here would be helpful. It's not clear what a Jenkins pipeline is.
Thanks for your comment. Actually i got this problem while working with Jenkins-AWS CLI to spin up my AWS infra and configuring EC2 instances with my custom script i used command as :- ssh -i mypemfile.pem -t ec2-user@IP 'ls -l && source myscript.sh && ./myscript.sh' Note:- In this case myscript.sh is at /home/ec2-user directory.
Ok, so you mean that you had the problem in Jenkins, not that you used Jenkins as a solution, right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.