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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Calum
  • 2,110
  • 2
  • 22
  • 39
  • 1
    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. – millimoose Feb 11 '13 at 23:40
  • @millimoose where $VIRTUAL_ENV is the path to the virtualenv? – Calum Feb 11 '13 at 23:47
  • Exactly, `activate` should set the same variable up so I used it figuring the name would be familiar. – millimoose Feb 11 '13 at 23:48
  • that doesnt seem to work either – Calum Feb 11 '13 at 23:55
  • Do you get any error messages? – millimoose Feb 11 '13 at 23:58
  • 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 failing – Calum Feb 12 '13 at 00:00
  • The suggestion by @millimoose 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/.log. – Jacob Wan Jul 04 '13 at 16:49

2 Answers2

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

pgSystemTester
  • 8,979
  • 2
  • 23
  • 49
Calum
  • 2,110
  • 2
  • 22
  • 39
  • Beyond reason, I've confirmed this on Ubuntu 12.04. Thank you – dbro Mar 12 '13 at 00:13
  • This has been driving me nuts. I'd give you five upvotes if I could. Thank you! – aendra May 31 '13 at 14:37
  • 2
    The reason this works is in my separate answer to this question. – Jeffrey Martinez Sep 30 '13 at 18:31
  • 3
    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...) – odigity Jan 11 '15 at 16:16
  • 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. – Max Candocia Mar 14 '18 at 20:10
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 .

Jeffrey Martinez
  • 4,384
  • 2
  • 31
  • 36