title: django-celery as a systemd service in virtualenv
body: <p>Is this possible to daemonize django-celery as a systemd service for a project in virtualenv?</p>
  
  <p>Here is my config:</p>
  
  <p>/etc/systemd/system/celery.service</p>
  
  <pre><code>[Unit]
  Description=Celery Service
  After=network.target
  
  [Service]
  Type=forking
  User=vagrant
  Group=vagrant
  Restart=no
  WorkingDirectory=/vagrant/myproj/
  ExecStart=/bin/sh -c '/var/www/vhost/myproj_env/bin/python \ 
     /vagrant/myproj/manage.py celery worker \ 
     --loglevel=DEBUG \
     --logfile=/var/log/celery/worker.log \ 
     --pidfile=/var/run/celery/worker.pid \
     -Q availability,celery --time-limit=300'
  ExecStop=/bin/sh -c '/var/www/vhost/myproj_env/bin/python \ 
      /vagrant/myproj/manage.py celery stop \
     --pidfile=/var/run/celery/worker.pid'
  
  [Install]
  WantedBy=multi-user.target
  </code></pre>
  
  <p>all the directories mentioned here do exist, and the permissions set correctly</p>
  
  <p>django-celery in settings.py:</p>
  
  <pre><code>INSTALLED_APPS = (
      ...
      'djcelery',
      'celery_haystack',
      ...
  )
  
  import djcelery
  djcelery.setup_loader()
  
  CELERYBEAT_SCHEDULER = "djcelery.schedulers.DatabaseScheduler"
  BROKER_URL = "redis://localhost:6379/0"
  CELERY_RESULT_BACKEND = BROKER_URL
  CELERY_REDIS_MAX_CONNECTIONS = 30
  </code></pre>
  
  <p>The following command starts celery normally, and I can see tasks being executed:</p>
  
  <pre><code>python manage.py celery worker --loglevel=INFO -Q availability,celery
  </code></pre>
  
  <p>Which is basically the same as specified in the service conf, except it posts to stdout</p>
  
  <p>However, when i try to <code>systemctl start celery.service</code>, it just fails silently: <code>systemctl status celery.service</code> reports inactive (dead)</p>
  
  <p>I'd be thankful for any hint on this problem. I might be missing something obvious, though i have a feeling that the process should not be so complicated, as it it now ((</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>The celery logs tell, that celery starts normally, but for some reason sysctl doesn't accept it. Here is a snippet from the celery log with --loglevel=DEBUG:</p>
  
  <pre><code>[2017-07-09 21:55:09,435: DEBUG/MainProcess] | Worker: Preparing bootsteps.
  [2017-07-09 21:55:09,439: DEBUG/MainProcess] | Worker: Building graph...
  [2017-07-09 21:55:09,439: DEBUG/MainProcess] | Worker: New boot order: {Beat, Timer, Hub, Queues (intra), Pool, Autoreloader, StateDB, Autoscaler, Consumer}
  ...
  [2017-07-09 21:55:10,696: DEBUG/MainProcess] ^-- substep ok
  [2017-07-09 21:55:10,696: DEBUG/MainProcess] | Consumer: Starting Heart
  [2017-07-09 21:55:10,697: DEBUG/MainProcess] ^-- substep ok
  [2017-07-09 21:55:10,697: DEBUG/MainProcess] | Consumer: Starting event loop
  [2017-07-09 21:55:10,697: WARNING/MainProcess] /var/www/vhost/myproj_env/local/lib/python2.7/site-packages/djcelery/loaders.py:130: UserWarning: Using settings.DEBUG leads to a memory leak, never use this setting in production environments!
    warn('Using settings.DEBUG leads to a memory leak, never '
  [2017-07-09 21:55:10,698: WARNING/MainProcess] celery@vagrant ready.
  [2017-07-09 21:55:10,698: DEBUG/MainProcess] | Worker: Hub.register Pool...
  [2017-07-09 21:55:10,699: DEBUG/MainProcess] basic.qos: prefetch_count-&gt;4
  </code></pre>
  
  <p>It is even processing tasks in the queue!</p>
  
  <p>But after 30 seconds or so sysctl fails to understand that worker is actually functioning normally, and closes it:</p>
  
  <pre><code>Job for celery.service failed because a timeout was exceeded. See "systemctl status celery.service" and "journalctl -xe" for details.
  
  vagrant@vagrant:~$ sudo systemctl status celery.service
  ● celery.service - Celery Service
     Loaded: loaded (/etc/systemd/system/celery.service; enabled; vendor preset: enabled)
     Active: failed (Result: timeout) since Sun 2017-07-09 21:56:38 UTC; 3min 0s ago
    Process: 3139 ExecStart=/bin/sh -c /var/www/vhost/myproj_env/bin/python /vagrant/myproj/manage.py c
      Tasks: 0
     Memory: 47.4M
        CPU: 1.532s
  
  Jul 09 21:55:08 vagrant systemd[1]: Starting Celery Service...
  Jul 09 21:56:38 vagrant systemd[1]: celery.service: Start operation timed out. Terminating.
  Jul 09 21:56:38 vagrant systemd[1]: Failed to start Celery Service.
  Jul 09 21:56:38 vagrant systemd[1]: celery.service: Unit entered failed state.
  Jul 09 21:56:38 vagrant systemd[1]: celery.service: Failed with result 'timeout'.
  </code></pre>
  
  <p>NB: I have reduced num of workers to one for simplicity, and edited conf files. This made the problem a bit clearer (it isn't a problem in celery, but probably in systemd), but still i have no clue, what's causing this timeout..</p>