Django Running Celery with Supervisor Running Supervisor


Example

  1. Create a script to start celery worker. Insert your script within your app. For example: stack/bin/celery_worker_start

     #!/bin/bash
     
     NAME="StackOverflow Project - celery_worker_start"
     
     PROJECT_DIR=/home/stackoverflow/apps/proj/proj/
     ENV_DIR=/home/stackoverflow/apps/proj/env/
     
     echo "Starting $NAME as `whoami`"
     
     # Activate the virtual environment
     cd "${PROJECT_DIR}"
     
     if [ -d "${ENV_DIR}" ]
     then
         . "${ENV_DIR}bin/activate"
     fi
     
     celery -A stack --loglevel='INFO'
    
  2. Add execution rights to your newly created script:

    chmod u+x bin/celery_worker_start

  3. Install supervisor (skip this test if supervisor already installed)

    apt-get install supervisor

  4. Add config file for your supervisor in order to start you celery. Place it in /etc/supervisor/conf.d/stack_supervisor.conf

     [program:stack-celery-worker]
     command = /home/stackoverflow/apps/stack/src/bin/celery_worker_start
     user = polsha
     stdout_logfile = /home/stackoverflow/apps/stack/src/logs/celery_worker.log
     redirect_stderr = true
     environment = LANG = en_US.UTF-8,LC_ALL = en_US.UTF-8
     numprocs = 1
     autostart = true
     autorestart = true
     startsecs = 10
     stopwaitsecs = 600
     priority = 998
    
  5. Reread and update supervisor

     sudo supervisorctl reread
         stack-celery-worker: available
     sudo supervisorctl update
         stack-celery-worker: added process group
    
  6. Basic commands

     sudo supervisorctl status stack-celery-worker                       
         stack-celery-worker      RUNNING    pid 18020, uptime 0:00:50
     sudo supervisorctl stop stack-celery-worker  
         stack-celery-worker: stopped
     sudo supervisorctl start stack-celery-worker                        
         stack-celery-worker: started
     sudo supervisorctl restart stack-celery-worker 
         stack-celery-worker: stopped
         stack-celery-worker: started