Celery定时任务的启动依赖于命令,有时需要指定很多的参数,造成很长的命令.如果重启服务器等,就需要重新用命令启动.这个时候问题就来了,重启服务器的人不知道命令怎么办?或者说定时任务有好几个,其中有一个定时任务已经运行一年了,是需要长期运行的,你忘记启动了,那就出问题了.
为了让定时任务在开机时自动启动,可以将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"
2.配置项说明 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
2.配置项说明 [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
配置好以上文件,并启动celery.service,后续重启服务器时celery定时任务就会自动启动.