why can't upstart run 'source bin/activate'?
Asked Answered
H

2

31

upstart won't activate my virtualenv for some reason.

This is how I run it

script
    # My startup script, plain old shell scripting here.
    cd path/to/env
    source bin/activate
    ....
end script

the virtualenv runs fine when started manually

Why does this not work?

Hardee answered 11/2, 2013 at 23:38 Comment(7)
It's possible upstart doesn't preserve the environment between successive commands? You can always use $VIRTUAL_ENV/bin/python ... to run your app directly without activating the environment.Arbitrator
@Arbitrator where $VIRTUAL_ENV is the path to the virtualenv?Hardee
Exactly, activate should set the same variable up so I used it figuring the name would be familiar.Arbitrator
that doesnt seem to work eitherHardee
Do you get any error messages?Arbitrator
no, but when i check the status after starting it, it is back at stop/waiting, have got code before and after that logs to a file so i know which line of code is failingHardee
The suggestion by @Arbitrator should have worked as long as the path to your virtual environment's python executable is correct. There's not much need to "activate" the environment unless you plan to issue multiple commands. In fact, if you can, just do "exec path/to/virtualenv/bin/python path/to/python/script". Do you have "console log" specified? Then you can see further output in /var/log/upstart/<your service>.log.Rentroll
H
53

So I've worked it out, for some reason upstart doesn't like using 'source' so I changed the line from:

source bin/activate

to

. bin/activate

and that works, don't know why though, so would be interested if someone could explain this

Hardee answered 12/2, 2013 at 0:11 Comment(5)
Beyond reason, I've confirmed this on Ubuntu 12.04. Thank youTrotskyite
This has been driving me nuts. I'd give you five upvotes if I could. Thank you!Responsibility
The reason this works is in my separate answer to this question.Aciniform
I'm tried both, and neither are working. (Ubuntu 14.04.1 Server.) (Also, not trying to source bin/activate, just trying to source the user's .bashrc to get env vars, using full path even...)Acceptance
I had this issue with gunicorn and upstart. I discovered that all I needed to do was to change the path of the gunicorn executable to the one in the environment's bin/ folder.Lawrencelawrencium
A
43

source is a bash built-in command but only a posix "special" command.

Upstart runs sh -e when executing the script sections.

sh shell doesn't understand source, only .

Aciniform answered 30/7, 2013 at 22:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.