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? Commented Aug 13, 2015 at 12:04
  • I'm afraid it's the latter Commented Aug 13, 2015 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? Commented Aug 13, 2015 at 12:13
  • Maybe I could. Haven't done such thing before. Commented Aug 13, 2015 at 12:16
  • works for my tests at least. Commented Aug 13, 2015 at 12:19

2 Answers 2

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? Commented Aug 13, 2015 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 Commented Aug 13, 2015 at 12:36
  • I meant something potentially using echo and piping Commented Aug 13, 2015 at 12:38
  • Well, guess there is no way to make it one line with this approach Commented Aug 13, 2015 at 12:41
  • like in these examples (especially point 4)? Commented Aug 13, 2015 at 12:44
0

Create the file ~/.ssh/rc. See https://man.openbsd.org/ssh.1#FILES

Inside that file, add the command you wanted to execute:

cd /path/to/dir
source s

Now this command will be invoked each time you ssh into your server

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.