5

I am trying to execute a script sitting on my remote server. This script activates a virtual environment and then executes some commands. Now I want to execute this script directly from my mac terminal. This is the content of the script

cd ~/workdir/
workon myvirtualenv
source ~/prodStuff/envVars 
git pull origin master
supervisorctl -u admin -p password restart gunicorn
supervisorctl -u admin -p password status
deactivate

This script works fine when I login to the server and execute it. But when I try to execute the same from mac's terminal, it gives me the following error

/home/ubuntu/scripts/deploy_code.sh: line 2: workon: command not found

So essentially, I am unable to switch to virtual environment. This is the command I am using to execute the script from my terminal

ssh prodserver ". /home/ubuntu/scripts/deploy_code.sh"

I will appreciate if someone can help me resolve this.

Thanks

Anurag
  • 1,521
  • 2
  • 17
  • 34
  • How and where is `workon` defined? – tripleee Nov 28 '15 at 07:22
  • workon is bash command only which activates the virtual environment. And it works if I login to my remote machine and then type in this command – Anurag Nov 28 '15 at 07:24
  • Yes. And how and where is this command defined? Is it an alias, a script, or something else? What is the output of `type workon`? – tripleee Nov 28 '15 at 07:26
  • Output of type workon shows "workon is a function" – Anurag Nov 28 '15 at 07:28
  • rewrite it in your favorite language? – hanshenrik Nov 28 '15 at 07:37
  • @tripleee got solution form your hint. The workon function was defined in the virtual-environment wrapper script. I sourced that file before executing workon and it worked. – Anurag Nov 28 '15 at 08:12
  • 2
    Possible duplicate of [ssh command execution doesn't consider .bashrc | .bash\_login | .ssh/rc?](http://stackoverflow.com/questions/1198378/ssh-command-execution-doesnt-consider-bashrc-bash-login-ssh-rc) – tripleee Nov 28 '15 at 08:31

1 Answers1

4

I had the same issue. I had conda virtual env which i would activate using : source activate py36. Being a conda env it was actually stored in /home/ubuntu/anaconda3/.

So the right way to activate this env was source /home/ubuntu/anaconda3/bin/activate py36. But since path to this env was stored in my system , which i verified with command echo $PATH. my server knew where to look when i wrote source activate py36. Unfortunately when you ssh from a remote machine, the server doesnt know where to look. so if you provide the entire path to the env like source /home/ubuntu/anaconda3/bin/activate py36 it will work.

You need to find out what kind of virtual env you have and then where its actually located.

Aseem
  • 5,848
  • 7
  • 45
  • 69