我们使用celery文档
使用systemd对celery进行守护http://docs.celeryproject.org/en/latest/userguide/daemonizing.html
以下是我们的systemd配置文件 -
[Unit]
Description=Celery Service
After=network.target
[Service]
Type=forking
User=ubuntu
Group=ubuntu
EnvironmentFile=/etc/default/celery/app
WorkingDirectory=/home/ubuntu/app/code/
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
环境文件如下 -
# Name of nodes to start
# here we have a single node
#CELERYD_NODES="w1"
# or we could have three nodes:
CELERYD_NODES="devw1 devw2 devw3"
# 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="app"
# or fully qualified:
#CELERY_APP="proj.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="DEBUG"
当我们执行以下命令时 -
sudo service celery start
服务已启动,但在控制台上没有提供任何错误消息时立即停止,下面是检查服务状态后的控制台 -
● celery - Celery Service
Loaded: loaded (/etc/systemd/system/celery.service; disabled; vendor preset: enabled)
Active: inactive (dead)
Jun 13 12:12:56 ip-10-0-5-99 sh[2248]: celery multi v3.1.18 (Cipater)
Jun 13 12:12:56 ip-10-0-5-99 sh[2248]: > Starting nodes...
Jun 13 12:12:56 ip-10-0-5-99 sh[2248]: > devw1@ip-10-0-5-99: OK
Jun 13 12:12:56 ip-10-0-5-99 sh[2248]: > devw2@ip-10-0-5-99: OK
Jun 13 12:12:56 ip-10-0-5-99 sh[2248]: > devw3@ip-10-0-5-99: OK
Jun 13 12:12:56 ip-10-0-5-99 systemd[1]: Started Celery Service.
Jun 13 12:12:58 ip-10-0-5-99 sh[2282]: celery multi v3.1.18 (Cipater)
Jun 13 12:12:58 ip-10-0-5-99 sh[2282]: > devw1@ip-10-0-5-99: DOWN
Jun 13 12:12:58 ip-10-0-5-99 sh[2282]: > devw2@ip-10-0-5-99: DOWN
Jun 13 12:12:58 ip-10-0-5-99 sh[2282]: > devw3@ip-10-0-5-99: DOWN
我们直接在工作目录中执行了start命令,下面是结果 - 命令:
/bin/sh -c '/usr/local/bin/celery multi start devw1 devw2 devw \ -A app --pidfile=/var/run/celery/%n.pid \ --logfile=/var/log/celery/%n%I.log --loglevel=DEBUG --time-limit=300 --concurrency=8'
输出
> --logfile=/var/log/celery/%n%I.log@ip-10-0-5-99: OK
Traceback (most recent call last):
File "/usr/lib/python2.7/runpy.py", line 174, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/usr/local/lib/python2.7/dist-packages/celery/__main__.py", line 54, in <module>
main()
File "/usr/local/lib/python2.7/dist-packages/celery/__main__.py", line 30, in main
main()
File "/usr/local/lib/python2.7/dist-packages/celery/bin/celery.py", line 81, in main
cmd.execute_from_commandline(argv)
File "/usr/local/lib/python2.7/dist-packages/celery/bin/celery.py", line 769, in execute_from_commandline
super(CeleryCommand, self).execute_from_commandline(argv)))
File "/usr/local/lib/python2.7/dist-packages/celery/bin/base.py", line 311, in execute_from_commandline
return self.handle_argv(self.prog_name, argv[1:])
File "/usr/local/lib/python2.7/dist-packages/celery/bin/celery.py", line 761, in handle_argv
return self.execute(command, argv)
File "/usr/local/lib/python2.7/dist-packages/celery/bin/celery.py", line 693, in execute
).run_from_argv(self.prog_name, argv[1:], command=argv[0])
File "/usr/local/lib/python2.7/dist-packages/celery/bin/worker.py", line 178, in run_from_argv
self.maybe_detach([command] + argv)
File "/usr/local/lib/python2.7/dist-packages/celery/bin/worker.py", line 185, in maybe_detach
detached_celeryd(self.app).execute_from_commandline(argv)
File "/usr/local/lib/python2.7/dist-packages/celery/bin/celeryd_detach.py", line 164, in execute_from_commandline
**vars(options)
File "/usr/local/lib/python2.7/dist-packages/celery/bin/celeryd_detach.py", line 44, in detach
with detached(logfile, pidfile, uid, gid, umask, working_directory, fake):
File "/usr/local/lib/python2.7/dist-packages/celery/platforms.py", line 321, in open
self.after_chdir()
File "/usr/local/lib/python2.7/dist-packages/celery/platforms.py", line 395, in after_chdir_do
_create_pidlock(pidfile).release()
File "/usr/local/lib/python2.7/dist-packages/celery/platforms.py", line 261, in _create_pidlock
if pidlock.is_locked() and not pidlock.remove_if_stale():
File "/usr/local/lib/python2.7/dist-packages/celery/platforms.py", line 189, in remove_if_stale
pid = self.read_pid()
File "/usr/local/lib/python2.7/dist-packages/celery/platforms.py", line 168, in read_pid
with open(self.path, 'r') as fh:
IOError: [Errno 21] Is a directory: '/var/run/celery'
/ var / run / celery是一个目录服务,能够在目录中创建pid文件,但在读取路径时却抛出了这个错误。
非常感谢任何帮助。