daemonizing.rst 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. # Name of nodes to start
  30. # here we have a single node
  31. CELERYD_NODES="w1"
  32. # or we could have three nodes:
  33. #CELERYD_NODES="w1 w2 w3"
  34. # Where to chdir at start.
  35. CELERYD_CHDIR="/opt/Myproject/"
  36. # Extra arguments to celeryd
  37. CELERYD_OPTS="--time-limit=300 --concurrency=8"
  38. # %n will be replaced with the nodename.
  39. CELERYD_LOG_FILE="/var/log/celery/%n.log"
  40. CELERYD_PID_FILE="/var/run/celery/%n.pid"
  41. # Workers should run as an unprivileged user.
  42. CELERYD_USER="celery"
  43. CELERYD_GROUP="celery"
  44. .. _generic-initd-celeryd-django-example:
  45. Example Django configuration
  46. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  47. This is an example configuration for those using `django-celery`::
  48. # Name of nodes to start, here we have a single node
  49. CELERYD_NODES="w1"
  50. # or we could have three nodes:
  51. #CELERYD_NODES="w1 w2 w3"
  52. # Where to chdir at start.
  53. CELERYD_CHDIR="/opt/Myproject/"
  54. # How to call "manage.py celeryd_multi"
  55. CELERYD_MULTI="$CELERYD_CHDIR/manage.py celeryd_multi"
  56. # How to call "manage.py celeryctl"
  57. CELERYCTL="$CELERYD_CHDIR/manage.py celeryctl"
  58. # Extra arguments to celeryd
  59. CELERYD_OPTS="--time-limit=300 --concurrency=8"
  60. # %n will be replaced with the nodename.
  61. CELERYD_LOG_FILE="/var/log/celery/%n.log"
  62. CELERYD_PID_FILE="/var/run/celery/%n.pid"
  63. # Workers should run as an unprivileged user.
  64. CELERYD_USER="celery"
  65. CELERYD_GROUP="celery"
  66. # Name of the projects settings module.
  67. export DJANGO_SETTINGS_MODULE="MyProject.settings"
  68. .. _generic-initd-celeryd-django-with-env-example:
  69. Example Django configuration Using Virtualenv
  70. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  71. In case you are using virtualenv, you should add the path to your
  72. environment's python interpreter::
  73. # Name of nodes to start, here we have a single node
  74. CELERYD_NODES="w1"
  75. # or we could have three nodes:
  76. #CELERYD_NODES="w1 w2 w3"
  77. # Where to chdir at start.
  78. CELERYD_CHDIR="/opt/Myproject/"
  79. # Python interpreter from environment.
  80. ENV_PYTHON="$CELERYD_CHDIR/env/bin/python"
  81. # How to call "manage.py celeryd_multi"
  82. CELERYD_MULTI="$ENV_PYTHON $CELERYD_CHDIR/manage.py celeryd_multi"
  83. # How to call "manage.py celeryctl"
  84. CELERYCTL="$ENV_PYTHON $CELERYD_CHDIR/manage.py celeryctl"
  85. # Extra arguments to celeryd
  86. CELERYD_OPTS="--time-limit=300 --concurrency=8"
  87. # %n will be replaced with the nodename.
  88. CELERYD_LOG_FILE="/var/log/celery/%n.log"
  89. CELERYD_PID_FILE="/var/run/celery/%n.pid"
  90. # Workers should run as an unprivileged user.
  91. CELERYD_USER="celery"
  92. CELERYD_GROUP="celery"
  93. # Name of the projects settings module.
  94. export DJANGO_SETTINGS_MODULE="MyProject.settings"
  95. .. _generic-initd-celeryd-options:
  96. Available options
  97. ~~~~~~~~~~~~~~~~~~
  98. * CELERYD_NODES
  99. Node names to start.
  100. * CELERYD_OPTS
  101. Additional arguments to celeryd, see `celeryd --help` for a list.
  102. * CELERYD_CHDIR
  103. Path to change directory to at start. Default is to stay in the current
  104. directory.
  105. * CELERYD_PID_FILE
  106. Full path to the PID file. Default is /var/run/celeryd%n.pid
  107. * CELERYD_LOG_FILE
  108. Full path to the celeryd log file. Default is /var/log/celeryd@%n.log
  109. * CELERYD_LOG_LEVEL
  110. Log level to use for celeryd. Default is INFO.
  111. * CELERYD_MULTI
  112. Path to the celeryd-multi program. Default is `celeryd-multi`.
  113. You can point this to a virtualenv, or even use manage.py for django.
  114. * CELERYCTL
  115. Path to the celeryctl program. Default is `celeryctl`.
  116. You can point this to a virtualenv, or even use manage.py for django.
  117. * CELERYD_USER
  118. User to run celeryd as. Default is current user.
  119. * CELERYD_GROUP
  120. Group to run celeryd as. Default is current user.
  121. .. _generic-initd-celerybeat:
  122. Init script: celerybeat
  123. -----------------------
  124. :Usage: `/etc/init.d/celerybeat {start|stop|restart}`
  125. :Configuration file: /etc/default/celerybeat or /etc/default/celeryd
  126. .. _generic-initd-celerybeat-example:
  127. Example configuration
  128. ~~~~~~~~~~~~~~~~~~~~~
  129. This is an example configuration for a Python project:
  130. `/etc/default/celerybeat`::
  131. # Where to chdir at start.
  132. CELERYBEAT_CHDIR="/opt/Myproject/"
  133. # Extra arguments to celerybeat
  134. CELERYBEAT_OPTS="--schedule=/var/run/celerybeat-schedule"
  135. .. _generic-initd-celerybeat-django-example:
  136. Example Django configuration
  137. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  138. This is an example configuration for those using `django-celery`
  139. `/etc/default/celerybeat`::
  140. # Where the Django project is.
  141. CELERYBEAT_CHDIR="/opt/Project/"
  142. # Name of the projects settings module.
  143. export DJANGO_SETTINGS_MODULE="settings"
  144. # Path to celerybeat
  145. CELERYBEAT="/opt/Project/manage.py celerybeat"
  146. # Extra arguments to celerybeat
  147. CELERYBEAT_OPTS="--schedule=/var/run/celerybeat-schedule"
  148. .. _generic-initd-celerybeat-options:
  149. Available options
  150. ~~~~~~~~~~~~~~~~~
  151. * CELERYBEAT_OPTS
  152. Additional arguments to celerybeat, see `celerybeat --help` for a
  153. list.
  154. * CELERYBEAT_PID_FILE
  155. Full path to the PID file. Default is /var/run/celeryd.pid.
  156. * CELERYBEAT_LOG_FILE
  157. Full path to the celeryd log file. Default is /var/log/celeryd.log
  158. * CELERYBEAT_LOG_LEVEL
  159. Log level to use for celeryd. Default is INFO.
  160. * CELERYBEAT
  161. Path to the celeryd program. Default is `celeryd`.
  162. You can point this to an virtualenv, or even use manage.py for django.
  163. * CELERYBEAT_USER
  164. User to run celeryd as. Default is current user.
  165. * CELERYBEAT_GROUP
  166. Group to run celeryd as. Default is current user.
  167. .. _generic-initd-troubleshooting:
  168. Troubleshooting
  169. ---------------
  170. If you can't get the init scripts to work, you should try running
  171. them in *verbose mode*::
  172. $ sh -x /etc/init.d/celeryd start
  173. This can reveal hints as to why the service won't start.
  174. Also you will see the commands generated, so you can try to run the celeryd
  175. command manually to read the resulting error output.
  176. For example my `sh -x` output does this:
  177. .. code-block:: bash
  178. ++ start-stop-daemon --start --chdir /opt/App/release/app --quiet \
  179. --oknodo --background --make-pidfile --pidfile /var/run/celeryd.pid \
  180. --exec /opt/App/release/app/manage.py celeryd -- --time-limit=300 \
  181. -f /var/log/celeryd.log -l INFO
  182. Run the celeryd command after `--exec` (without the `--`) to show the
  183. actual resulting output:
  184. .. code-block:: bash
  185. $ /opt/App/release/app/manage.py celeryd --time-limit=300 \
  186. -f /var/log/celeryd.log -l INFO
  187. .. _daemon-supervisord:
  188. `supervisord`_
  189. ==============
  190. * `extra/supervisord/`_
  191. .. _`extra/supervisord/`:
  192. http://github.com/celery/celery/tree/3.0/extra/supervisord/
  193. .. _`supervisord`: http://supervisord.org/
  194. .. _daemon-launchd:
  195. launchd (OS X)
  196. ==============
  197. * `extra/mac/`_
  198. .. _`extra/mac/`:
  199. http://github.com/celery/celery/tree/3.0/extra/mac/
  200. .. _daemon-windows:
  201. Windows
  202. =======
  203. See this excellent external tutorial:
  204. http://www.calazan.com/windows-tip-run-applications-in-the-background-using-task-scheduler/
  205. CentOS
  206. ======
  207. In CentOS we can take advantage of built-in service helpers, such as the
  208. pid-based status checker function in ``/etc/init.d/functions``.
  209. See the sample script in http://github.com/celery/celery/tree/3.0/extra/centos/.