这是我使用在 Ubuntu 16.04 LTS 上运行的较新 systemd 的工作配置。芹菜在一个虚拟环境中。应用程序是 Python/Flask。
系统文件:/etc/systemd/system/celery.service
您需要更改用户和路径。
[Unit]
Description=Celery Service
After=network.target
[Service]
Type=forking
User=nick
Group=nick
EnvironmentFile=-/home/nick/myapp/server_configs/celery_env.conf
WorkingDirectory=/home/nick/myapp
ExecStart=/bin/sh -c '${CELERY_BIN} multi start ${CELERYD_NODES} \
-A ${CELERY_APP} --pidfile=${CELERYD_PID_FILE} \
--logfile=${CELERYD_LOG_FILE} --loglevel=${CELERYD_LOG_LEVEL} ${CELERYD_OPTS}'
ExecStop=/bin/sh -c '${CELERY_BIN} multi stopwait ${CELERYD_NODES} \
--pidfile=${CELERYD_PID_FILE}'
ExecReload=/bin/sh -c '${CELERY_BIN} multi restart ${CELERYD_NODES} \
-A ${CELERY_APP} --pidfile=${CELERYD_PID_FILE} \
--logfile=${CELERYD_LOG_FILE} --loglevel=${CELERYD_LOG_LEVEL} ${CELERYD_OPTS}'
[Install]
WantedBy=multi-user.target
环境文件(上面引用):/home/nick/myapp/server_configs/celery_env.conf
# Name of nodes to start
# here we have a single node
CELERYD_NODES="w1"
# or we could have three nodes:
#CELERYD_NODES="w1 w2 w3"
# Absolute or relative path to the 'celery' command:
CELERY_BIN="/home/nick/myapp/venv/bin/celery"
# App instance to use
CELERY_APP="myapp.tasks"
# 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"
要为您的用户自动创建具有正确权限的日志和运行文件夹,请在/usr/lib/tmpfiles.d
. 我/var/run/celery
在重新启动时删除了文件夹,然后 celery 无法正常启动。
我的/usr/lib/tmpfiles.d/celery.conf
文件:
d /var/log/celery 2775 nick nick -
d /var/run/celery 2775 nick nick -
启用:sudo systemctl enable celery.service
现在您需要重新启动系统才能创建/var/log/celery
和文件夹。/var/run/celery
您可以通过检查登录来检查 celery 是否在重新启动后启动/var/log/celery
。
要重新启动 celery:sudo systemctl restart celery.service
调试:tail -f /var/log/syslog
并尝试重新启动 celery 以查看错误是什么。它可能与后端或其他事物有关。
希望这可以帮助!