5

I've got a script, named s, on a remote server that activates a virtual environment like this:

source venv/bin/activate

When SSH-ed into the server, I'm able to activate the environment by

. s

And I SSH into the server as following:

ssh -t user@host "cd /path/to/dir ; /bin/bash"

In addition to changing the working directory to /path/to/dir it would be nice if I could activate the environment right away each time I log into the server. But no matter where I put . s into the the SSH command, using -c for /bin/bash or not, the session always ends immediately.

7
  • is adding it to the user's .bashrc an option or should the souring only occur when sshing into the machine?
    – FelixJN
    Aug 13 '15 at 12:04
  • I'm afraid it's the latter Aug 13 '15 at 12:05
  • 1
    could you use /bin/bash --rcfile all.rc in the ssh command and in all.rc define both the standard bashrc and your special s sourcing?
    – FelixJN
    Aug 13 '15 at 12:13
  • Maybe I could. Haven't done such thing before. Aug 13 '15 at 12:16
  • works for my tests at least.
    – FelixJN
    Aug 13 '15 at 12:19
6

Use a new source file e.g. /home/user/.rcforssh

 #rc for ssh
 . /home/user/.bashrc
 . /home/user/venv/bin/activate  #or whichever location

and log in with

ssh -t user@host "cd /path/to/dir ; /bin/bash --rcfile /home/user/.rcforssh"

Side note: source is not POSIX, while . is.


UPDATE following discussion specifying OP's needs:

For creating and removing the the modified source file rcforssh on the fly, on can use:

ssh -t user@host "cd /path/to/dir ; echo '. ~/.bashrc ; . s ; rm rcforssh' > rcforssh ; /bin/bash --rcfile rcforssh"

i.e. creating the source file with an echo command and adding the removal in said file.

10
  • Can I load the bashrc file from the standard input on the fly so that it all becomes a single command line? Aug 13 '15 at 12:30
  • well, you could add the cd /path/to/dir in the .rcforssh source file. If that is what you meant? So you'll only end up with the bash command in the ssh
    – FelixJN
    Aug 13 '15 at 12:36
  • I meant something potentially using echo and piping Aug 13 '15 at 12:38
  • Well, guess there is no way to make it one line with this approach Aug 13 '15 at 12:41
  • like in these examples (especially point 4)?
    – FelixJN
    Aug 13 '15 at 12:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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