Mese Mese - 6 days ago 6
Linux Question

Starting virtualenv script rc local

I'm very newbie in this field and hope someone can help me.

So I have a backend project which I need to launch automatically when the computer switches on (I really don't care how, using systemd or rc.local, my boss told me rc.local, but I guess either will do). I just need to start a docker container, then start my virtualenv and then run the project.

So far I've tried this at

/etc/rc.local


docker start cassandratt #my docker container
sleep 20 #an ugly hack to give time for the container to start
cd /home/backend/
. venv/bin/activate
. /run.py


It doesn't work, but the docker container starts, so I guess the problem is around virtualenv or python, I really don't know as I don't have any experience on this field.

Any idea on how I could accomplish it?

Thanks in advance

Edit:

Following Samer's guidance I tried creating a folder after activating the virtualenv and it created fine, so I suppore the problem is trying to execute run.py, perhaps loading the virtualenv's python?

docker start cassandratt #my docker container
cd /home/backend/
. venv/bin/activate
mkdir test #folder created fine
. /run.py
mkdir test2 #folder not created

Answer

So, partially, the solution seems to set some variables instead of accesing them directly. At least this worked for me. Thanks Samer for giving us a big tip :)

HOME=/home/backend #the project path docker start container . $HOME/venv/bin/activate #activates the virtualenv of the project /usr/bin/env python $HOME/run.py & #runs the run.py through virtualenv's python #and runs it in the background exit 0

Comments