celery后台运行-使用init-script的方法

参考链接:https://abnerzhao.com/2018/03/25/celery-background/

写此博文,是为了让自己和别人有个借鉴

过多的我这里不多说,因为上面链接中有,只说需要设置的地方

首先是在 /etc/default/ 下建立一个 celeryd 文件

在服务器端,cd 到以上目录中,vim celeryd 进行编写(也可以在本地写完后,scp上去,scp参考链接:https://linuxtools-rst.readthedocs.io/zh_CN/latest/tool/scp.html

我的celeryd中的代码如下:

# Names of nodes to start
#   most people will only start one node:
CELERYD_NODES="root"  # work节点,我做了修改的部分,这里填写了我的服务器的用户,
#   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="/root/anaconda3/envs/scrapy_py3/bin/celery"  # 这里是我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_tasks.tasks"  # 我的tasks.py文件存放在了celery_tasks文件夹中,所以此处做了如此的修改
# or fully qualified:
#CELERY_APP="root.tasks:app"  # 这里我没有测试,小伙伴们可以自己测试一下
# Where to chdir at start.
CELERYD_CHDIR="/root/data/project/inchange/"  # 这是我存放项目的路径,也是celery_tasks文件夹所在的路径
# 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="root"  # 这里我填写的是服务器的用户名
CELERYD_GROUP="root"  # 这里我填写的是服务器的用户名
# If enabled pid and log directories will be created if missing,
# and owned by the userid/group configured.
CELERY_CREATE_DIRS=1  # 这里我没有修改

其次,是将最顶部链接中提到的两个文件进行修改,存放在 /etc/init.d/ 下

celeryd修改的部分代码:

.......
# 此段代码,大约在文件的54行的位置
DEFAULT_USER="root"  # 这里对应前面自己建立的celeryd文件填写的
DEFAULT_PID_FILE="/var/run/celery/%n.pid"  # 我未做修改
DEFAULT_LOG_FILE="/var/log/celery/%n.log"  # 我未做修改
DEFAULT_LOG_LEVEL="INFO"  # 我未做修改
DEFAULT_NODES="root"  # 这里对应前面自己建立的celeryd文件填写的
DEFAULT_CELERYD="-m celery worker --detach"  # 我未做修改
.......

celerybeat修改的部分代码:

......
# 此段代码大约在文件108行
CELERY_BIN=${CELERY_BIN:-"celery"}
DEFAULT_USER="root"  # 在此处做了修改,和上一个类似
DEFAULT_PID_FILE="/var/run/celery/beat.pid"
DEFAULT_LOG_FILE="/var/log/celery/beat.log"
DEFAULT_LOG_LEVEL="INFO"
DEFAULT_CELERYBEAT="$CELERY_BIN beat"
......

最后启动,停止,参看状态

# 启动
<sudo> /etc/init.d/celeryd start
<sudo> /etc/init.d/celerybeat start
# 停止
<sudo> /etc/init.d/celeryd stop
<sudo> /etc/init.d/celerybeat stop
# 查看状态
<sudo> /etc/init.d/celeryd status
<sudo> /etc/init.d/celerybeat status

我也尝试过supervisor,但是celery执行会有问题,不能进行下去,如果有朋友成功了,希望能多多分享

在使用supervisor中,遇到过如下bug:

1,Error: Another program is already listening on a port that one of our HTTP servers is configured to use.  Shut this program down first before starting supervisord

2,unix:///tmp/supervisor.sock no such file

3,Unlinking stale socket /var/run/supervisor/supervisor.sock

以上bug找了很多方法,改了,但是效果不大,现在试试init的方法,后续有问题继续更新。。。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值