Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are my environment variables not detected when starting up celery?

I am running django on centos served by apache and mod_wsgi. I followed the instructions to set up celery to be run as a daemon.

I put this init script https://github.com/celery/celery/blob/3.1/extra/generic-init.d/celeryd in /etc/init.d/celeryd

and set up the configuration in

/etc/default/celeryd 

I am using environment variables in my django settings.py file so I can use different configurations in my development and production environments. I know these environment variables are set correctly because the app has been working this whole time. I think that celery is just not getting the variable passed to it or something.

I checked by typing the env command. variables are showing fine.

To start up I just do:

service celeryd start

It tries to start up but throws an error saying that I do not have my environment variables set.

I wrote a function to grab environment variables. that is what throws the error.

def get_env_variable(var_name):
    try:
        return os.environ[var_name]
    except KeyError:
        error_msg = "Set the %s environment variable" % var_name
        raise ImproperlyConfigured(error_msg)

The only way that error is thrown is if the environment variable is not set correctly.

Does anyone know why celery is not detecting the enironment variables that I have set?

like image 665
Spencer Cooley Avatar asked Mar 31 '14 10:03

Spencer Cooley


People also ask

How do I enable environment variables?

On the Windows taskbar, right-click the Windows icon and select System. In the Settings window, under Related Settings, click Advanced system settings. On the Advanced tab, click Environment Variables. Click New to create a new environment variable.

How can I test if my environment variable is working?

To Check if an Environment Variable ExistsSelect Start > All Programs > Accessories > Command Prompt. In the command window that opens, enter echo %VARIABLE%. Replace VARIABLE with the name of the environment variable. For example, to check if NUKE_DISK_CACHE is set, enter echo %NUKE_DISK_CACHE%.

How do I get all environment variables?

To list all the environment variables, use the command " env " (or " printenv "). You could also use " set " to list all the variables, including all local variables.


2 Answers

I just discovered that I not only had to set my environment variables in the system, but I also had to pass those variables in to the /etc/default/celleryd script.

I just put my variables at the bottom of /etc/default/celleryd:

export MY_SPECIAL_VARIABLE = "my production variable"
export MY_OTHERSPECIAL_VARIABLE = "my other production variable"
like image 167
Spencer Cooley Avatar answered Sep 25 '22 14:09

Spencer Cooley


if environment variables write on ~/.bashrc, you can add source ~/.bashrc to /etc/init.d/celeryd at first.

like image 35
Balicanta Avatar answered Sep 21 '22 14:09

Balicanta