Celery增加Systemd配置

Celery增加Systemd配置

一、设置python celery项目的配置

  1. 在/etc/conf.d/目录创建celery文件,编辑
# Name of nodes to start
# here we have a single node
CELERYD_NODES="work"
# or we could have three nodes:
#CELERYD_NODES="w1 w2 w3"
# Absolute or relative path to the 'celery' command:
CELERY_BIN="/usr/local/bin/celery"
#CELERY_BIN="/virtualenvs/def/bin/celery"
# App instance to use
# comment out this line if you don't use an app
CELERY_APP="celery_crontab"
# or fully qualified:
#CELERY_APP="main.tasks:app"
# How to call manage.py
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.
# - %I will be replaced with the current child process index
# and is important when using the prefork pool to avoid race conditions.
CELERYD_PID_FILE="/var/run/celery/%n.pid"
CELERYD_LOG_FILE="/var/log/celery/%n%I.log"
CELERYD_LOG_LEVEL="INFO"
  1. 配置项说明
    2.1 CELERYD_NODES=“work”,启动的celery进程的进程名为work
    2.2 CELERY_BIN="/usr/local/bin/celery",celery安装目录,可使用which celery找到
    2.3 CELERY_APP=“celery_crontab” ,在celery项目的配置文件config.py中,创建Celery实例对象APP时给APP定义的名字,一定要保持一致,如:
# config.py中创建celery对象时的命名
app = Celery('celery_crontab', broker='amqp://guest@localhost//')

二、设置systemd配置

  1. 在/etc/systemd/system/目录创建celery.service文件,编辑
[Unit]
Description=Celery Service
After=network.target
[Service]
Type=forking
User=celery
Group=celery
EnvironmentFile=/etc/conf.d/celery
WorkingDirectory=/root/celery_crontab
ExecStart=/usr/local/bin/celery multi start work -A main -l info -B --
logfile=celerylog.log
ExecStop=/usr/local/bin/celery multi stop work -A main -l info -B --
logfile=celerylog.log
ExecReload=/usr/local/bin/celery multi restat work -A main -l info -B --
logfile=celerylog.log
[Install]
WantedBy=multi-user.target
  1. 配置项说明
    [Unit]
    Description:对当前服务的简单描述,如说明一下功能
    After:表示celery.service应该在network.target后启动
    [Service]
    Type:定义启动类型,forking表示以fork()方式启动
    User:指定启动任务的用户(提前创建好用户和所属组,设置好用户权限)
    Group:指定用户的组
    EnvironmentFile:指定celery项目的systemd配置文件:/etc/conf.d/celery
    WorkingDirectory:指定celery项目的启动目录,项目启动文件main.py所在目录
    ExecStart:在执行systemctl start celery.service命令时,会执行ExecStart
    ExecStop:在执行systemctl stop celery.service命令时,会执行ExecStop
    ExecReload:在执行systemctl restart celery.service命令时,会执行ExecReload
    [Install]
    WantedBy=multi-user.target:表示重启系统后自动启动celery.service

三、使用systemd运行celery.service

  1. 重载配置文件
    每次修改celery.service配置后都要执行此命令,以便systemd确认该文件
    systemctl daemon-reload
  2. 启动命令
    systemctl start celery.service
  3. 停止命令
    systemctl stop celery.service
  4. 重启命令
    systemctl restart celery.service
  5. 查看celery.service的运行状态
    systemctl status celery.service

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小斌哥ge

非常感谢,祝你一切顺利。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值