Hello everyone, I’m doing this post because I just got stuck in this a while ago and thought it would be nice to share.
Django-Celery
First of all you need to install it and configure the basics(this all is well explained in the docs), so basically you can do:
Then you need to install a broker server(like RabbitMQ):
After this is done, you need to put some basic configurations for celery in your settings file, like this:
Also don’t forget to run syncdb or migrate(if using south).
Now if you want to run celery, you do it like:
Daemonizing…Not so fast!
This part is not so intuitive for those who are starting to use this tool, but after you get what need to be done, it’s all fast and furious.
This is, in my opinion, because of some misleading details in the celery daemon docs. So I’ll try to make it as explained as I can.
Create a Configuration File For The Daemon
In this file there will be some variable used to run the celery as a daemon, like:
Where is the project folder;
How many workers;
Where will be the log files…
And many others.
But it will also contain any environment variable that you need in your project!
So if you have a different settings for development and for production using an env var, you’ll have to set this in the configuration file as well.
Or else you’ll might and up having the celery working with the sqlite3.
You create this file as: /etc/default/celeryd
And put something like this inside it:
Get a init.d script
Yeah, this is strange, but after some time I understood this.
So basically the file that they tell you to run in the docs, you need to get and put it there yourself…
And this init.d script will run the celeryd using the configurations that you’ll set in the /etc/default/celeryd.
You can get the file in:
https://raw.github.com/ask/celery/1da3aa43d1e6de525beeda398d0acb8841d5b4d2/contrib/generic-init.d/celeryd
This file must go to /etc/init.d/celeryd
And don’t forget to make it executable: chmod +x /etc/init.d/celeryd
Using the Daemon
To use it, just do like this:
Some Details
So, if you run the celery without the daemon and have no problem, but when running the daemon it tells you that some celery table was not created, then probably your celeryd is not using the correct database.
Also don’t forget that the CELERYD_USER and CELERYD_GROUP must have permission to use the manage.py, and to access the CELERYD_LOG_FILE and CELERYD_PID_FILE folders!
And of course. You need to create the folders for CELERYD_LOG_FILE and CELERYD_PID_FILE.
And again, don’t change the CELERY_CONFIG_MODULE=”celeryconfig”. Since you’re using django-celery there is no celeryconfig.py in your project, but you need to set this so that it will get the default one(that it’s in the celery package).
That’s it! Hope this will help others that where lost in this(especially in the CELERY_CONFIG_MODULE) and in the init.d script.


Good post! I stumbled upon it looking for ways to daemonize Celery. I followed the instructions on the official doc and also on your post and I get the daemon running but the tasks do not get registered and the log says ERROR/MainProcess] Received unregistered task of type ‘likes.tasks.test’. I keep investigating, if I launch the process manually then it works flawlessly, it’s only when daemonized that it fails. If I find out why I’ll let you know.
Hi bastien, thanks for the comment.
Hope you find out what’s going on (have you tried changing to a little older version of celery?) I remember that once I had problem with the one that was more recent, and fixed it by reverting to a oldder one.
It wasn’t the same problem you’re facing, but still, I think you should give it a shot.
Also can you tell what is the cmd that you’re using to invoke the celeryd, maybe you’re also having some problem with the permissions.
Well I ended up using supervisord and it works well for now.
This is my supervisord conf file:
[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor ; (‘AUTO’ child log dir, default $TEMP)
; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///var/run//supervisor.sock ; use a unix:// URL for a unix socket
; The [include] section can just contain the “files” setting. This
; setting can list multiple files (separated by whitespace or
; newlines). It can also contain wildcards. The filenames are
; interpreted as relative to this file. Included files *cannot*
; include files themselves.
[include]
files = /etc/supervisor/conf.d/*.conf
And this is my celery conf file that is in the include directory:
[program:celery]
command=/path/to/your/virtualenv/bin/python /path/to/your/project/manage.py celeryd –loglevel=INFO
environment=PYTHONPATH=’/path/to/virtualenv/yourproject’
directory=/path/to/virtualenv/yourproject
user=choose an unprivileged user here
numprocs=1
stdout_logfile=/var/log/celery/django.log
stderr_logfile=/var/log/celery/django.log
autostart=true
autorestart=true
startsecs=10
stopwaitsecs = 600
priority=998
hope it helps!
Hi, Great tutorial. When I do
“sudo /etc/init.d/celeryd status”
I get the error message:
Error: No nodes replied within time constraint
Have you come across that?
Thank you
I beleave I got this message when the celeryd was not running.
After that I just restarted:
/etc/init.d/celeryd stopt
/etc/init.d/celeryd start
and then the status option worked just fine.
Your article saved my day. Awesome!
Question: I’m using virtual environment and when environment is enabled, I have celeryctl and celeryd-multi. But when I run /etc/init.d/celeryd_kart (renamed for the environment), it says:
/etc/init.d/celeryd_kart: 177: /etc/init.d/celeryd_kart: celeryctl: not found
/etc/init.d/celeryd_kart: 134: /etc/init.d/celeryd_kart: celeryd-multi: not found
What am I missing?
Thanks a lot!
Eras
Not sure I understood what you said.
If you use rename the file to be without the _kart it worked, but after you renamed it stopped?