daemonizing.rst 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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 :program:`celeryd`,
  14. that should run on Linux, FreeBSD, OpenBSD, and other Unix platforms.
  15. .. _`extra/generic-init.d/`:
  16. http://github.com/celery/celery/tree/3.0/extra/generic-init.d/
  17. .. _generic-initd-celeryd:
  18. Init script: celeryd
  19. --------------------
  20. :Usage: `/etc/init.d/celeryd {start|stop|restart|status}`
  21. :Configuration file: /etc/default/celeryd
  22. To configure celeryd you probably need to at least tell it where to change
  23. directory to when it starts (to find your `celeryconfig`).
  24. .. _generic-initd-celeryd-example:
  25. Example configuration
  26. ~~~~~~~~~~~~~~~~~~~~~
  27. This is an example configuration for a Python project.
  28. :file:`/etc/default/celeryd`:
  29. .. code-block:: bash
  30. # Name of nodes to start
  31. # here we have a single node
  32. CELERYD_NODES="w1"
  33. # or we could have three nodes:
  34. #CELERYD_NODES="w1 w2 w3"
  35. # Absolute or relative path to the 'celery' command:
  36. CELERY_BIN="/usr/local/bin/celery"
  37. #CELERY_BIN="/virtualenvs/def/bin/celery"
  38. # App instance to use
  39. # comment out this line if you don't use an app
  40. CELERY_APP="proj"
  41. # or fully qualified:
  42. #CELERY_APP="proj.tasks:app"
  43. # Where to chdir at start.
  44. CELERYD_CHDIR="/opt/Myproject/"
  45. # Extra arguments to celeryd
  46. CELERYD_OPTS="--time-limit=300 --concurrency=8"
  47. # Name of the celery config module.
  48. CELERY_CONFIG_MODULE="celeryconfig"
  49. # %n will be replaced with the nodename.
  50. CELERYD_LOG_FILE="/var/log/celery/%n.log"
  51. CELERYD_PID_FILE="/var/run/celery/%n.pid"
  52. # Workers should run as an unprivileged user.
  53. CELERYD_USER="celery"
  54. CELERYD_GROUP="celery"
  55. .. _generic-initd-celeryd-django-example:
  56. Example Django configuration
  57. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  58. This is an example configuration for those using `django-celery`:
  59. .. code-block:: bash
  60. # Name of nodes to start, here we have a single node
  61. CELERYD_NODES="w1"
  62. # or we could have three nodes:
  63. #CELERYD_NODES="w1 w2 w3"
  64. # Where to chdir at start.
  65. CELERYD_CHDIR="/opt/Myproject/"
  66. # How to call "manage.py celery"
  67. CELERY_BIN="$CELERYD_CHDIR/manage.py celery"
  68. # Extra arguments to celeryd
  69. CELERYD_OPTS="--time-limit=300 --concurrency=8"
  70. # %n will be replaced with the nodename.
  71. CELERYD_LOG_FILE="/var/log/celery/%n.log"
  72. CELERYD_PID_FILE="/var/run/celery/%n.pid"
  73. # Workers should run as an unprivileged user.
  74. CELERYD_USER="celery"
  75. CELERYD_GROUP="celery"
  76. # Name of the projects settings module.
  77. export DJANGO_SETTINGS_MODULE="MyProject.settings"
  78. .. _generic-initd-celeryd-django-with-env-example:
  79. Example Django configuration Using Virtualenv
  80. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  81. In case you are using virtualenv, you should add the path to your
  82. environment's python interpreter:
  83. .. code-block:: bash
  84. # Name of nodes to start, here we have a single node
  85. CELERYD_NODES="w1"
  86. # or we could have three nodes:
  87. #CELERYD_NODES="w1 w2 w3"
  88. # Where to chdir at start.
  89. CELERYD_CHDIR="/opt/Myproject/"
  90. # Python interpreter from environment.
  91. ENV_PYTHON="$CELERYD_CHDIR/env/bin/python"
  92. # How to call "manage.py celery"
  93. CELERY_BIN="$ENV_PYTHON $CELERYD_CHDIR/manage.py celery"
  94. # Extra arguments to celeryd
  95. CELERYD_OPTS="--time-limit=300 --concurrency=8"
  96. # Name of the celery config module.
  97. CELERY_CONFIG_MODULE="celeryconfig"
  98. # %n will be replaced with the nodename.
  99. CELERYD_LOG_FILE="/var/log/celery/%n.log"
  100. CELERYD_PID_FILE="/var/run/celery/%n.pid"
  101. # Workers should run as an unprivileged user.
  102. CELERYD_USER="celery"
  103. CELERYD_GROUP="celery"
  104. # Name of the projects settings module.
  105. export DJANGO_SETTINGS_MODULE="MyProject.settings"
  106. .. _generic-initd-celeryd-options:
  107. Available options
  108. ~~~~~~~~~~~~~~~~~~
  109. * CELERY_APP
  110. App instance to use (value for ``--app`` argument).
  111. * CELERY_BIN
  112. Absolute or relative path to the :program:`celery` program.
  113. Examples:
  114. * :file:`celery``
  115. * :file:`/usr/local/bin/celery`
  116. * :file:`/virtualenvs/proj/bin/celery`
  117. * :file:`/virtualenvs/proj/bin/python -m celery`
  118. * CELERYD_NODES
  119. Node names to start.
  120. * CELERYD_OPTS
  121. Additional arguments to celeryd, see `celeryd --help` for a list.
  122. * CELERYD_CHDIR
  123. Path to change directory to at start. Default is to stay in the current
  124. directory.
  125. * CELERYD_PID_FILE
  126. Full path to the PID file. Default is /var/run/celeryd%n.pid
  127. * CELERYD_LOG_FILE
  128. Full path to the celeryd log file. Default is /var/log/celeryd@%n.log
  129. * CELERYD_LOG_LEVEL
  130. Log level to use for celeryd. Default is INFO.
  131. * CELERYD_USER
  132. User to run celeryd as. Default is current user.
  133. * CELERYD_GROUP
  134. Group to run celeryd as. Default is current user.
  135. * CELERY_CREATE_DIRS
  136. Always create directories (log directory and pid file directory).
  137. Default is to only create directories when no custom logfile/pidfile set.
  138. * CELERY_CREATE_RUNDIR
  139. Always create pidfile directory. By default only enabled when no custom
  140. pidfile location set.
  141. * CELERY_CREATE_LOGDIR
  142. Always create logfile directory. By default only enable when no custom
  143. logfile location set.
  144. .. _generic-initd-celerybeat:
  145. Init script: celerybeat
  146. -----------------------
  147. :Usage: `/etc/init.d/celerybeat {start|stop|restart}`
  148. :Configuration file: /etc/default/celerybeat or /etc/default/celeryd
  149. .. _generic-initd-celerybeat-example:
  150. Example configuration
  151. ~~~~~~~~~~~~~~~~~~~~~
  152. This is an example configuration for a Python project:
  153. `/etc/default/celerybeat`:
  154. .. code-block:: bash
  155. # Absolute or relative path to the 'celery' command:
  156. CELERY_BIN="/usr/local/bin/celery"
  157. #CELERY_BIN="/virtualenvs/def/bin/celery"
  158. # App instance to use
  159. # comment out this line if you don't use an app
  160. CELERY_APP="proj"
  161. # or fully qualified:
  162. #CELERY_APP="proj.tasks:app"
  163. # Where to chdir at start.
  164. CELERYBEAT_CHDIR="/opt/Myproject/"
  165. # Extra arguments to celerybeat
  166. CELERYBEAT_OPTS="--schedule=/var/run/celerybeat-schedule"
  167. .. _generic-initd-celerybeat-django-example:
  168. Example Django configuration
  169. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  170. This is an example configuration for those using `django-celery`
  171. `/etc/default/celerybeat`::
  172. # Where the Django project is.
  173. CELERYBEAT_CHDIR="/opt/Project/"
  174. # Name of the projects settings module.
  175. export DJANGO_SETTINGS_MODULE="settings"
  176. # Path to celery command
  177. CELERY_BIN="/opt/Project/manage.py celery"
  178. # Extra arguments to celerybeat
  179. CELERYBEAT_OPTS="--schedule=/var/run/celerybeat-schedule"
  180. .. _generic-initd-celerybeat-options:
  181. Available options
  182. ~~~~~~~~~~~~~~~~~
  183. * CELERY_APP
  184. App instance to use (value for ``--app`` argument).
  185. * CELERY_BIN
  186. Absolute or relative path to the :program:`celery` program.
  187. Examples:
  188. * :file:`celery``
  189. * :file:`/usr/local/bin/celery`
  190. * :file:`/virtualenvs/proj/bin/celery`
  191. * :file:`/virtualenvs/proj/bin/python -m celery`
  192. * CELERYBEAT_OPTS
  193. Additional arguments to celerybeat, see `celerybeat --help` for a
  194. list.
  195. * CELERYBEAT_PID_FILE
  196. Full path to the PID file. Default is /var/run/celeryd.pid.
  197. * CELERYBEAT_LOG_FILE
  198. Full path to the celeryd log file. Default is /var/log/celeryd.log
  199. * CELERYBEAT_LOG_LEVEL
  200. Log level to use for celeryd. Default is INFO.
  201. * CELERYBEAT
  202. Path to the celeryd program. Default is `celeryd`.
  203. You can point this to an virtualenv, or even use manage.py for django.
  204. * CELERYBEAT_USER
  205. User to run celeryd as. Default is current user.
  206. * CELERYBEAT_GROUP
  207. Group to run celeryd as. Default is current user.
  208. * CELERY_CREATE_DIRS
  209. Always create directories (log directory and pid file directory).
  210. Default is to only create directories when no custom logfile/pidfile set.
  211. * CELERY_CREATE_RUNDIR
  212. Always create pidfile directory. By default only enabled when no custom
  213. pidfile location set.
  214. * CELERY_CREATE_LOGDIR
  215. Always create logfile directory. By default only enable when no custom
  216. logfile location set.
  217. .. _generic-initd-troubleshooting:
  218. Troubleshooting
  219. ---------------
  220. If you can't get the init scripts to work, you should try running
  221. them in *verbose mode*::
  222. $ sh -x /etc/init.d/celeryd start
  223. This can reveal hints as to why the service won't start.
  224. Also you will see the commands generated, so you can try to run the celeryd
  225. command manually to read the resulting error output.
  226. For example my `sh -x` output does this:
  227. .. code-block:: bash
  228. ++ start-stop-daemon --start --chdir /opt/App/release/app --quiet \
  229. --oknodo --background --make-pidfile --pidfile /var/run/celeryd.pid \
  230. --exec /opt/App/release/app/manage.py celeryd -- --time-limit=300 \
  231. -f /var/log/celeryd.log -l INFO
  232. Run the celeryd command after `--exec` (without the `--`) to show the
  233. actual resulting output:
  234. .. code-block:: bash
  235. $ /opt/App/release/app/manage.py celeryd --time-limit=300 \
  236. -f /var/log/celeryd.log -l INFO
  237. .. _daemon-supervisord:
  238. `supervisord`_
  239. ==============
  240. * `extra/supervisord/`_
  241. .. _`extra/supervisord/`:
  242. http://github.com/celery/celery/tree/3.0/extra/supervisord/
  243. .. _`supervisord`: http://supervisord.org/
  244. .. _daemon-launchd:
  245. launchd (OS X)
  246. ==============
  247. * `extra/mac/`_
  248. .. _`extra/mac/`:
  249. http://github.com/celery/celery/tree/3.0/extra/mac/
  250. .. _daemon-windows:
  251. Windows
  252. =======
  253. See this excellent external tutorial:
  254. http://www.calazan.com/windows-tip-run-applications-in-the-background-using-task-scheduler/