title: Daemonization celery in production
body: <p>I'm trying to run Celery as service in Ubuntu 18.04, using Django 2.1.1 and Celery 4.1.1 that is installed in <code>env</code> and Celery 4.1.0 installed systemwide. I'm going forward with <a href="http://docs.celeryproject.org/en/latest/userguide/daemonizing.html#daemon-systemd-generic" rel="nofollow noreferrer">this tutorial</a> to run Celery as service. Here is my Django's project tree:</p>
  
  <pre><code>    hamclassy-backend
                   ├── apps
                   ├── env
                   │   └── bin
                   │        └── celery
                   ├── hamclassy
                   │   ├── celery.py
                   │   ├── __init__.py
                   │   ├── settings-dev.py
                   │   ├── settings.py
                   │   └── urls.py
                   ├── wsgi.py
                   ├── manage.py
                   └── media
  </code></pre>
  
  <p>Here is celery.py :</p>
  
  <pre><code>from __future__ import absolute_import, unicode_literals
  import os
  from celery import Celery
  os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'hamclassy.settings')
  
  app = Celery('hamclassy')
  
  app.config_from_object('django.conf:settings', namespace='CELERY')
  app.autodiscover_tasks()
  </code></pre>
  
  <p>Here is /etc/systemd/system/celery.service:</p>
  
  <pre><code>[Unit]
  Description=Celery Service
  After=network.target
  
  [Service]
  Type=forking
  User=classgram
  Group=www-data
  EnvironmentFile=/etc/conf.d/celery
  WorkingDirectory=/home/classgram/www/hamclassy-backend
  ExecStart=/bin/sh -c '${CELERY_BIN} multi start ${CELERYD_NODES} \
    -A ${CELERY_APP} --pidfile=${CELERYD_PID_FILE} \
    --logfile=${CELERYD_LOG_FILE} --loglevel=${CELERYD_LOG_LEVEL} ${CELERYD_OPTS}'
  ExecStop=/bin/sh -c '${CELERY_BIN} multi stopwait ${CELERYD_NODES} \
    --pidfile=${CELERYD_PID_FILE}'
  ExecReload=/bin/sh -c '${CELERY_BIN} multi restart ${CELERYD_NODES} \
    -A ${CELERY_APP} --pidfile=${CELERYD_PID_FILE} \
    --logfile=${CELERYD_LOG_FILE} --loglevel=${CELERYD_LOG_LEVEL} ${CELERYD_OPTS}'
  
  [Install]
  WantedBy=multi-user.target
  
  
  </code></pre>
  
  <p>and /etc/conf.d/celery:</p>
  
  <pre><code>CELERYD_NODES="celery-worker"
  
  CELERY_BIN="/home/classgram/www/env/bin/celery"
  
  # App instance to use
  CELERY_APP="hamclassy"
  
  
  CELERYD_MULTI="multi"
  # Extra command-line arguments to the worker
  CELERYD_OPTS="--time-limit=300 --concurrency=8"
  
  # %n will be replaced with the first part of the nodename.
  CELERYD_LOG_FILE="/var/log/celery/%n%I.log"
  CELERYD_PID_FILE="/var/run/celery/%n.pid"
  CELERYD_LOG_LEVEL="INFO"
  
  </code></pre>
  
  <p>When I run service with <code>systemctl start celery.service</code> the following error appears:</p>
  
  <blockquote>
    <p>Job for celery.service failed because the control process exited with
    error code. See "systemctl status celery.service" and "journalctl -xe"
    for detail</p>
  </blockquote>
  
  <p>When I run <code>sudo journalctl -b -u celery</code> the below log appears:</p>
  
  <blockquote>
    <p>Starting Celery Service... celery.service: Control process exited,
    code=exited status=200 celery.service: Failed with result 'exit-code'.
    Failed to start Celery Service.</p>
  </blockquote>
  
  <p><strong>For more information</strong>: 
  <br/><strong>1)</strong> When I use <code>groups classgram</code> this appears: <code>www-data sudo</code>. 
  <br/><strong>2)</strong> I can run Celery with <code>celery -A hamclassy worker -l info</code> after activating environment very well.</p>
  
  <p>Thank you</p>