0

I use a bash script to run gunicorn. It is named _run_gunicorn.sh_

#!/bin/bash
NAME=new_project 
DJANGODIR=/home/flame/Projects/$NAME
SOCKFILE=/home/flame/launch/web.sock
USER=flame                               
GROUP=flame                                                     
DJANGO_SETTINGS_MODULE=$NAME.settings 
DJANGO_WSGI_MODULE=$NAME.wsgi

# export PWD=$DJANGODIR   # still not work if I uncomment THIS LINE

RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR
gunicorn ${DJANGO_WSGI_MODULE}:application \
  --name $NAME \
  --workers 7 \
  --user=$USER --group=$GROUP \
  --log-level=debug \
  --bind=unix:$SOCKFILE

If I run from the project dir:

[/home/flame/Projects/new_project]$ bash run_gunicorn.sh

It works well. But if

[~]$ bash Projects/new_project/run_gunicorn.sh

it raises errors:

gunicorn.errors.HaltServer: <HaltServer 'Worker failed to boot.' 3>

I guess it is about current working directory. So I change the add export PWD=$DJANGODIR before gunicorn run. But the error remains.

Is it about some python related environment variables? Or what's the problem?

Frozen Flame
  • 3,135
  • 2
  • 23
  • 35

1 Answers1

3

Using

export PWD=$DJANGODIR

you do NOT actually change your current working directory. You can easily check this in a shell by using the command pwd after the set. You will have to include something like

cd $DJANGODIR

into your script.

Marcus Rickert
  • 4,138
  • 3
  • 24
  • 29
  • Learnt! But still curious about the difference about the two. Isn't a program the the current working directory from the environmental variable PWD of the shell process from where it starts? Or via other way? – Frozen Flame Dec 22 '13 at 11:25
  • According to the manual page of the bash the env variable `PWD` is automatically set by the shell. This way you can cheaply find out about your current working directory. It's a little strange that this env variable is not read-only. So you can change it but the shell won't change the working directory for you automatically. – Marcus Rickert Dec 22 '13 at 11:32
  • If you change the directory inside a script, the change is local to that script only. It doesn't change the directory of the shell that started the script. – robertklep Dec 22 '13 at 12:01
  • @robertklep: Thanks for pointing this out. I mixed this up with sourcing a script in which case it is necessary. I removed the addendum to my answer. – Marcus Rickert Dec 22 '13 at 12:21
  • @robertklep I understand this, e.g. a script with one line `cd newdir` endup with done nothing. But I assume the PWD will be passed to programs started inside the script. Unfortunately it doesn't.... – Frozen Flame Dec 22 '13 at 12:43
  • @MarcusRickert yes, if you source the script things are different :) – robertklep Dec 22 '13 at 12:49
  • @frozen-flame it's similar to `export UID=0`; doing that doesn't mean any programs will suddenly be run as root :) `$PWD` and `$UID` are just convenience variables. – robertklep Dec 22 '13 at 12:53