daemonizing.rst 9.6 KB

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