【问题标题】:Run CELERY as Daemon in Ubuntu在 Ubuntu 中将 CELERY 作为守护进程运行
【发布时间】:2014-04-26 21:01:20
【问题描述】:

如何在 ubuntu 中将 celery 作为守护程序服务运行,而不是每次都运行“celery -A projname worker -l info”命令。

我用的是celery 3.1.8版本....

【问题讨论】:

标签: ubuntu celery daemon


【解决方案1】:

您可以使用 celery 仓库中的 init.d script 将 celery 制作成守护进程(将其保存到 /etc/init.d/)。您还需要创建一个configuration file 以便脚本加载到/etc/default/celeryd

这是完整的celery doc

【讨论】:

    【解决方案2】:

    您可以使用名为 supervisord 的工具将 celery worker 作为守护进程。

    这是一个简单的流程管理器, 您可以找到一个示例here,了解如何使用主管的配置文件配置您的工作人员。

    【讨论】:

      【解决方案3】:

      这是很久以前提出的问题,但我认为值得给出答案。

      您可以按照以下步骤操作:

      1. 创建一个 celery 用户
      sudo adduser celery
      
      1. 创建初始化脚本
      sudo nano celeryd
      

      然后将celeryd 和过去复制到创建的celeryd。 然后

      chmod +x celeryd
      sudo mv celeryd /etc/init.d/
      
      1. 创建配置文件
      • 为初始化脚本创建空的 /etc/default/celeryd 配置文件
      sudo touch /etc/default/celeryd
      sudo vim /etc/default/celeryd
      
      • 根据您的项目编辑上述 celeryd。
       # Names of nodes to start
       #   most people will only start one node:
       CELERYD_NODES="worker1"
       #   but you can also start multiple and configure settings
       #   for each in CELERYD_OPTS
       #CELERYD_NODES="worker1 worker2 worker3"
       #   alternatively, you can specify the number of nodes to start:
       #CELERYD_NODES=10
      
       # Absolute or relative path to the 'celery' command:
       #CELERY_BIN="/usr/local/bin/celery"
       #CELERY_BIN="/virtualenvs/def/bin/celery"
       CELERY_BIN="/opt/django_projects/your_proj/your_env/bin/celery"
      
       # App instance to use
       # comment out this line if you don't use an app
       CELERY_APP="your_proj"
       # or fully qualified:
       #CELERY_APP="proj.tasks:app"
      
       # Where to chdir at start.
       CELERYD_CHDIR="/opt/django_projects/your_proj/"
      
       # Extra command-line arguments to the worker
       CELERYD_OPTS="--time-limit=300 --concurrency=8"
       # Configure node-specific settings by appending node name to arguments:
       #CELERYD_OPTS="--time-limit=300 -c 8 -c:worker2 4 -c:worker3 2 -Ofair:worker1"
      
       # Set logging level to DEBUG
       #CELERYD_LOG_LEVEL="DEBUG"
      
       # %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"
      
       # Workers should run as an unprivileged user.
       #   You need to create this user manually (or you can choose
       #   a user/group combination that already exists (e.g., nobody).
       CELERYD_USER="celery"
       CELERYD_GROUP="celery"
      
       # If enabled pid and log directories will be created if missing,
       # and owned by the userid/group configured.
       CELERY_CREATE_DIRS=1
      
      1. 创建日志目录
      sudo mkdir /var/log/celery
      sudo chown -R celery: /var/log/celery
      
      1. 详细说明初始化脚本
      sudo sh -x /etc/init.d/celeryd start
      
      1. 启用守护进程
      sudo update-rc.d celeryd defaults
      sudo service celeryd start
      

      嗯,希望对你有帮助。 谢谢。

      【讨论】:

      • 感谢您的分步指导!!我在第 5 步遇到错误,说“/bin/bash: multi: command not found”
      最近更新 更多