daemonizing.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. .. _daemonizing:
  2. ================================
  3. Running the worker as a daemon
  4. ================================
  5. Celery does not daemonize itself, please use one of the following
  6. daemonization tools.
  7. .. contents::
  8. :local:
  9. .. _daemon-generic:
  10. Generic init scripts
  11. ====================
  12. See the `extra/generic-init.d/`_ directory Celery distribution.
  13. This directory contains generic bash init scripts for the
  14. :program:`celery worker` program,
  15. these should run on Linux, FreeBSD, OpenBSD, and other Unix-like platforms.
  16. .. _`extra/generic-init.d/`:
  17. http://github.com/celery/celery/tree/3.1/extra/generic-init.d/
  18. .. _generic-initd-celeryd:
  19. Init script: celeryd
  20. --------------------
  21. :Usage: `/etc/init.d/celeryd {start|stop|restart|status}`
  22. :Configuration file: /etc/default/celeryd
  23. To configure this script to run the worker properly you probably need to at least
  24. tell it where to change
  25. directory to when it starts (to find the module containing your app, or your
  26. configuration module).
  27. The daemonization script is configured by the file ``/etc/default/celeryd``,
  28. which is a shell (sh) script. You can add environment variables and the
  29. configuration options below to this file. To add environment variables you
  30. must also export them (e.g. ``export DISPLAY=":0"``)
  31. .. Admonition:: Superuser privileges required
  32. The init scripts can only be used by root,
  33. and the shell configuration file must also be owned by root.
  34. Unprivileged users do not need to use the init script,
  35. instead they can use the :program:`celery multi` utility (or
  36. :program:`celery worker --detach`):
  37. .. code-block:: bash
  38. $ celery multi start worker1 \
  39. --pidfile="$HOME/run/celery/%n.pid" \
  40. --logfile="$HOME/log/celery/%n.log"
  41. $ celery multi restart worker1 --pidfile="$HOME/run/celery/%n.pid"
  42. $ celery multi stopwait worker1 --pidfile="$HOME/run/celery/%n.pid"
  43. .. _generic-initd-celeryd-example:
  44. Example configuration
  45. ~~~~~~~~~~~~~~~~~~~~~
  46. This is an example configuration for a Python project.
  47. :file:`/etc/default/celeryd`:
  48. .. code-block:: bash
  49. # Names of nodes to start
  50. # most will only start one node:
  51. CELERYD_NODES="worker1"
  52. # but you can also start multiple and configure settings
  53. # for each in CELERYD_OPTS (see `celery multi --help` for examples).
  54. CELERYD_NODES="worker1 worker2 worker3"
  55. # Absolute or relative path to the 'celery' command:
  56. CELERY_BIN="/usr/local/bin/celery"
  57. #CELERY_BIN="/virtualenvs/def/bin/celery"
  58. # App instance to use
  59. # comment out this line if you don't use an app
  60. CELERY_APP="proj"
  61. # or fully qualified:
  62. #CELERY_APP="proj.tasks:app"
  63. # Where to chdir at start.
  64. CELERYD_CHDIR="/opt/Myproject/"
  65. # Extra command-line arguments to the worker
  66. CELERYD_OPTS="--time-limit=300 --concurrency=8"
  67. # %N will be replaced with the first part of the nodename.
  68. CELERYD_LOG_FILE="/var/log/celery/%N.log"
  69. CELERYD_PID_FILE="/var/run/celery/%N.pid"
  70. # Workers should run as an unprivileged user.
  71. # You need to create this user manually (or you can choose
  72. # a user/group combination that already exists, e.g. nobody).
  73. CELERYD_USER="celery"
  74. CELERYD_GROUP="celery"
  75. # If enabled pid and log directories will be created if missing,
  76. # and owned by the userid/group configured.
  77. CELERY_CREATE_DIRS=1
  78. .. _generic-initd-celeryd-django-example:
  79. Example Django configuration
  80. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  81. Django users now uses the exact same template as above,
  82. but make sure that the module that defines your Celery app instance
  83. also sets a default value for :envvar:`DJANGO_SETTINGS_MODULE`
  84. as shown in the example Django project in :ref:`django-first-steps`.
  85. .. _generic-initd-celeryd-options:
  86. Available options
  87. ~~~~~~~~~~~~~~~~~~
  88. * CELERY_APP
  89. App instance to use (value for ``--app`` argument).
  90. If you're still using the old API, or django-celery, then you
  91. can omit this setting.
  92. * CELERY_BIN
  93. Absolute or relative path to the :program:`celery` program.
  94. Examples:
  95. * :file:`celery`
  96. * :file:`/usr/local/bin/celery`
  97. * :file:`/virtualenvs/proj/bin/celery`
  98. * :file:`/virtualenvs/proj/bin/python -m celery`
  99. * CELERYD_NODES
  100. List of node names to start (separated by space).
  101. * CELERYD_OPTS
  102. Additional command-line arguments for the worker, see
  103. `celery worker --help` for a list. This also supports the extended
  104. syntax used by `multi` to configure settings for individual nodes.
  105. See `celery multi --help` for some multi-node configuration examples.
  106. * CELERYD_CHDIR
  107. Path to change directory to at start. Default is to stay in the current
  108. directory.
  109. * CELERYD_PID_FILE
  110. Full path to the PID file. Default is /var/run/celery/%N.pid
  111. * CELERYD_LOG_FILE
  112. Full path to the worker log file. Default is /var/log/celery/%N.log
  113. * CELERYD_LOG_LEVEL
  114. Worker log level. Default is INFO.
  115. * CELERYD_USER
  116. User to run the worker as. Default is current user.
  117. * CELERYD_GROUP
  118. Group to run worker as. Default is current user.
  119. * CELERY_CREATE_DIRS
  120. Always create directories (log directory and pid file directory).
  121. Default is to only create directories when no custom logfile/pidfile set.
  122. * CELERY_CREATE_RUNDIR
  123. Always create pidfile directory. By default only enabled when no custom
  124. pidfile location set.
  125. * CELERY_CREATE_LOGDIR
  126. Always create logfile directory. By default only enable when no custom
  127. logfile location set.
  128. .. _generic-initd-celerybeat:
  129. Init script: celerybeat
  130. -----------------------
  131. :Usage: `/etc/init.d/celerybeat {start|stop|restart}`
  132. :Configuration file: /etc/default/celerybeat or /etc/default/celeryd
  133. .. _generic-initd-celerybeat-example:
  134. Example configuration
  135. ~~~~~~~~~~~~~~~~~~~~~
  136. This is an example configuration for a Python project:
  137. `/etc/default/celerybeat`:
  138. .. code-block:: bash
  139. # Absolute or relative path to the 'celery' command:
  140. CELERY_BIN="/usr/local/bin/celery"
  141. #CELERY_BIN="/virtualenvs/def/bin/celery"
  142. # App instance to use
  143. # comment out this line if you don't use an app
  144. CELERY_APP="proj"
  145. # or fully qualified:
  146. #CELERY_APP="proj.tasks:app"
  147. # Where to chdir at start.
  148. CELERYBEAT_CHDIR="/opt/Myproject/"
  149. # Extra arguments to celerybeat
  150. CELERYBEAT_OPTS="--schedule=/var/run/celerybeat-schedule"
  151. .. _generic-initd-celerybeat-django-example:
  152. Example Django configuration
  153. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  154. You should use the same template as above, but make sure the
  155. ``DJANGO_SETTINGS_MODULE`` variable is set (and exported), and that
  156. ``CELERYD_CHDIR`` is set to the projects directory:
  157. .. code-block:: bash
  158. export DJANGO_SETTINGS_MODULE="settings"
  159. CELERYD_CHDIR="/opt/MyProject"
  160. .. _generic-initd-celerybeat-options:
  161. Available options
  162. ~~~~~~~~~~~~~~~~~
  163. * CELERY_APP
  164. App instance to use (value for ``--app`` argument).
  165. * CELERYBEAT_OPTS
  166. Additional arguments to celerybeat, see `celerybeat --help` for a
  167. list.
  168. * CELERYBEAT_PID_FILE
  169. Full path to the PID file. Default is /var/run/celeryd.pid.
  170. * CELERYBEAT_LOG_FILE
  171. Full path to the celeryd log file. Default is /var/log/celeryd.log
  172. * CELERYBEAT_LOG_LEVEL
  173. Log level to use for celeryd. Default is INFO.
  174. * CELERYBEAT_USER
  175. User to run beat as. Default is current user.
  176. * CELERYBEAT_GROUP
  177. Group to run beat as. Default is current user.
  178. * CELERY_CREATE_DIRS
  179. Always create directories (log directory and pid file directory).
  180. Default is to only create directories when no custom logfile/pidfile set.
  181. * CELERY_CREATE_RUNDIR
  182. Always create pidfile directory. By default only enabled when no custom
  183. pidfile location set.
  184. * CELERY_CREATE_LOGDIR
  185. Always create logfile directory. By default only enable when no custom
  186. logfile location set.
  187. .. _daemon-systemd-generic:
  188. Usage systemd
  189. =============
  190. .. _generic-systemd-celery:
  191. Service file: celery.service
  192. ----------------------------
  193. :Usage: `systemctl {start|stop|restart|status} celery.service`
  194. :Configuration file: /etc/conf.d/celery
  195. To create a temporary folders for the log and pid files change user and group in
  196. /usr/lib/tmpfiles.d/celery.conf.
  197. To configure user, group, chdir change settings User, Group and WorkingDirectory defines
  198. in /usr/lib/systemd/system/celery.service.
  199. .. _generic-systemd-celery-example:
  200. Example configuration
  201. ~~~~~~~~~~~~~~~~~~~~~
  202. This is an example configuration for a Python project:
  203. :file:`/etc/conf.d/celery`:
  204. .. code-block:: bash
  205. # Name of nodes to start
  206. # here we have a single node
  207. CELERYD_NODES="w1"
  208. # or we could have three nodes:
  209. #CELERYD_NODES="w1 w2 w3"
  210. # Absolute or relative path to the 'celery' command:
  211. CELERY_BIN="/usr/local/bin/celery"
  212. #CELERY_BIN="/virtualenvs/def/bin/celery"
  213. # How to call manage.py
  214. CELERYD_MULTI="multi"
  215. # Extra command-line arguments to the worker
  216. CELERYD_OPTS="--time-limit=300 --concurrency=8"
  217. # %N will be replaced with the first part of the nodename.
  218. CELERYD_LOG_FILE="/var/log/celery/%N.log"
  219. CELERYD_PID_FILE="/var/run/celery/%N.pid"
  220. .. _generic-systemd-celeryd-django-example:
  221. Example Django configuration
  222. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  223. This is an example configuration for those using `django-celery`:
  224. .. code-block:: bash
  225. # Name of nodes to start
  226. # here we have a single node
  227. CELERYD_NODES="w1"
  228. # or we could have three nodes:
  229. #CELERYD_NODES="w1 w2 w3"
  230. # Absolute path to "manage.py"
  231. CELERY_BIN="/opt/Myproject/manage.py"
  232. # How to call manage.py
  233. CELERYD_MULTI="celery multi"
  234. # Extra command-line arguments to the worker
  235. CELERYD_OPTS="--time-limit=300 --concurrency=8"
  236. # %N will be replaced with the first part of the nodename.
  237. CELERYD_LOG_FILE="/var/log/celery/%N.log"
  238. CELERYD_PID_FILE="/var/run/celery/%N.pid"
  239. To add an environment variable such as DJANGO_SETTINGS_MODULE use the
  240. Environment in celery.service.
  241. .. _generic-initd-troubleshooting:
  242. Troubleshooting
  243. ---------------
  244. If you can't get the init scripts to work, you should try running
  245. them in *verbose mode*:
  246. .. code-block:: bash
  247. # sh -x /etc/init.d/celeryd start
  248. This can reveal hints as to why the service won't start.
  249. If the worker starts with "OK" but exits almost immediately afterwards
  250. and there is nothing in the log file, then there is probably an error
  251. but as the daemons standard outputs are already closed you'll
  252. not be able to see them anywhere. For this situation you can use
  253. the :envvar:`C_FAKEFORK` environment variable to skip the
  254. daemonization step:
  255. .. code-block:: bash
  256. C_FAKEFORK=1 sh -x /etc/init.d/celeryd start
  257. and now you should be able to see the errors.
  258. Commonly such errors are caused by insufficient permissions
  259. to read from, or write to a file, and also by syntax errors
  260. in configuration modules, user modules, 3rd party libraries,
  261. or even from Celery itself (if you've found a bug, in which case
  262. you should :ref:`report it <reporting-bugs>`).
  263. .. _daemon-supervisord:
  264. `supervisord`_
  265. ==============
  266. * `extra/supervisord/`_
  267. .. _`extra/supervisord/`:
  268. http://github.com/celery/celery/tree/3.1/extra/supervisord/
  269. .. _`supervisord`: http://supervisord.org/
  270. .. _daemon-launchd:
  271. launchd (OS X)
  272. ==============
  273. * `extra/osx`_
  274. .. _`extra/osx`:
  275. http://github.com/celery/celery/tree/3.1/extra/osx/
  276. .. _daemon-windows:
  277. Windows
  278. =======
  279. See this excellent external tutorial:
  280. http://www.calazan.com/windows-tip-run-applications-in-the-background-using-task-scheduler/
  281. CentOS
  282. ======
  283. In CentOS we can take advantage of built-in service helpers, such as the
  284. pid-based status checker function in ``/etc/init.d/functions``.
  285. See the sample script in http://github.com/celery/celery/tree/3.1/extra/centos/.