Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

When I run my program interactively, it works fine:

ssh somehost
$ ~/some/path/somescript.py

When I run my program over ssh directly, it doesn't work. The variable PYTHONPATH is not set, because .bashrc is not sourced.

ssh somehost ~/some/path/somescript.py

If I run ssh somehost 'source ~/.bashrc; ~/some/path/somescript.py', it works fine.

But the latter wouldn't work for some other folks, e.g. using tcsh and not having a ~/.bashrc at all).

What is the command to run a thing via ssh on another host that works for all shells?

share|improve this question
1  
Answered here, stackoverflow.com/questions/820517/bashrc-at-ssh-login, basically, ssh doesn't source .bashrc, you need to source your .bashrc via .bash_profile or other options. –  EightBitTony Jun 17 '11 at 22:49
    
@EightBitTony: No, that's a different issue. bzdjamboo's question is about a non-interactive session. –  Gilles Jun 17 '11 at 23:16
    
Actually the last answer in the link I posted appears to be valuable, ssh does source .bashrc, but it doesn't execute due to the check for being interactive. I appreciate however, that your answer covers all the bases. –  EightBitTony Jun 17 '11 at 23:27
1  
@EightBitTony: On that question, the accepted answer is a good solution for the asker but the answers don't tell the full story. SSH itself doesn't source anything. If your login shell is bash invoked as bash, then it sources .bash_profile or .profile for an interactive login, and .bashrc for a non-interactive login. If your login shell is bash invoked as sh or ash or ksh, it sources .profile for an interactive login and nothing for a non-interactive login. –  Gilles Jun 18 '11 at 7:45

1 Answer 1

There is no standard per-user file that is run for non-interactive logins. You need to either make the program self-contained, so that it's able to find its dependencies without relying on non-default environment variables, or else explicitly set the environment, typically with

ssh somehost '. ~/.profile; exec ~/some/path/somescript.py'

You shouldn't be setting environment variables in .bashrc: this file is meant for interactive shells, and is read in each instance of bash. Environment variables should be set in .profile, which is read when you log in. Apart from the very small number of people who don't use a Bourne-style shell as their login shell, .profile does work for everyone, whether they use bash or zsh or csh or fish interactively. See also Difference between .bashrc and .bash_profile, Which setup files should be used for setting up environment variables with bash?.

That being said, there is a way to source a script every time you log in over ssh with a particular key. See Is there a way to push shell config information when SSHing to a host?, sh startup files over ssh.

share|improve this answer
    
This is awesome, thanks a lot! –  bzdjamboo Jun 18 '11 at 4:25
    
Now how do I mark it "answered"? :-) –  bzdjamboo Jun 18 '11 at 4:26
    
Along the left side of this answer section there should be up and down vote arrows and a vote counter. Just below that there should be the outline of a checkbox. You can click that to activate the answer accept function. –  Caleb Jun 18 '11 at 6:16
    
Thankl you all again, but apparently your answers didn't solve my problem. –  bzdjamboo Jun 20 '11 at 18:33
    
Here is a bit newer formulation of the problem and my current understanding of the root of it. –  bzdjamboo Jun 20 '11 at 18:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.