daemonizing.rst 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. # Name of the celery config module.
  50. CELERY_CONFIG_MODULE="celeryconfig"
  51. # %N will be replaced with the first part of the nodename.
  52. CELERYD_LOG_FILE="/var/log/celery/%N.log"
  53. CELERYD_PID_FILE="/var/run/celery/%N.pid"
  54. # Workers should run as an unprivileged user.
  55. CELERYD_USER="celery"
  56. CELERYD_GROUP="celery"
  57. .. _generic-initd-celeryd-django-example:
  58. Example Django configuration
  59. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  60. This is an example configuration for those using `django-celery`:
  61. .. code-block:: bash
  62. # Name of nodes to start, here we have a single node
  63. CELERYD_NODES="w1"
  64. # or we could have three nodes:
  65. #CELERYD_NODES="w1 w2 w3"
  66. # Where to chdir at start.
  67. CELERYD_CHDIR="/opt/Myproject/"
  68. # How to call "manage.py celery"
  69. CELERY_BIN="$CELERYD_CHDIR/manage.py celery"
  70. # Extra command-line arguments for the worker (see celery worker --help).
  71. CELERYD_OPTS="--time-limit=300 --concurrency=8"
  72. # %n will be replaced with the nodename.
  73. CELERYD_LOG_FILE="/var/log/celery/%n.log"
  74. CELERYD_PID_FILE="/var/run/celery/%n.pid"
  75. # Workers should run as an unprivileged user.
  76. CELERYD_USER="celery"
  77. CELERYD_GROUP="celery"
  78. # Name of the projects settings module.
  79. export DJANGO_SETTINGS_MODULE="MyProject.settings"
  80. .. _generic-initd-celeryd-django-with-env-example:
  81. Example Django configuration Using Virtualenv
  82. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  83. In case you are using virtualenv, you should add the path to your
  84. environment's python interpreter:
  85. .. code-block:: bash
  86. # Name of nodes to start, here we have a single node
  87. CELERYD_NODES="w1"
  88. # or we could have three nodes:
  89. #CELERYD_NODES="w1 w2 w3"
  90. # Where to chdir at start.
  91. CELERYD_CHDIR="/opt/Myproject/"
  92. # Python interpreter from environment.
  93. ENV_PYTHON="$CELERYD_CHDIR/env/bin/python"
  94. # How to call "manage.py celery"
  95. CELERY_BIN="$ENV_PYTHON $CELERYD_CHDIR/manage.py celery"
  96. # Extra command-line arguments to the worker (see celery worker --help)
  97. CELERYD_OPTS="--time-limit=300 --concurrency=8"
  98. # Name of the celery config module.
  99. CELERY_CONFIG_MODULE="celeryconfig"
  100. # %n will be replaced with the nodename.
  101. CELERYD_LOG_FILE="/var/log/celery/%n.log"
  102. CELERYD_PID_FILE="/var/run/celery/%n.pid"
  103. # Workers should run as an unprivileged user.
  104. CELERYD_USER="celery"
  105. CELERYD_GROUP="celery"
  106. # Name of the projects settings module.
  107. export DJANGO_SETTINGS_MODULE="MyProject.settings"
  108. .. _generic-initd-celeryd-options:
  109. Available options
  110. ~~~~~~~~~~~~~~~~~~
  111. * CELERY_APP
  112. App instance to use (value for ``--app`` argument).
  113. * CELERY_BIN
  114. Absolute or relative path to the :program:`celery` program.
  115. Examples:
  116. * :file:`celery``
  117. * :file:`/usr/local/bin/celery`
  118. * :file:`/virtualenvs/proj/bin/celery`
  119. * :file:`/virtualenvs/proj/bin/python -m celery`
  120. * CELERYD_NODES
  121. Node names to start.
  122. * CELERYD_OPTS
  123. Additional command-line arguments for the worker, see
  124. `celery worker --help` for a list.
  125. * CELERYD_CHDIR
  126. Path to change directory to at start. Default is to stay in the current
  127. directory.
  128. * CELERYD_PID_FILE
  129. Full path to the PID file. Default is /var/run/celery/%N.pid
  130. * CELERYD_LOG_FILE
  131. Full path to the worker log file. Default is /var/log/celery/%N.log
  132. * CELERYD_LOG_LEVEL
  133. Worker log level. Default is INFO.
  134. * CELERYD_USER
  135. User to run the worker as. Default is current user.
  136. * CELERYD_GROUP
  137. Group to run worker as. Default is current user.
  138. * CELERY_CREATE_DIRS
  139. Always create directories (log directory and pid file directory).
  140. Default is to only create directories when no custom logfile/pidfile set.
  141. * CELERY_CREATE_RUNDIR
  142. Always create pidfile directory. By default only enabled when no custom
  143. pidfile location set.
  144. * CELERY_CREATE_LOGDIR
  145. Always create logfile directory. By default only enable when no custom
  146. logfile location set.
  147. .. _generic-initd-celerybeat:
  148. Init script: celerybeat
  149. -----------------------
  150. :Usage: `/etc/init.d/celerybeat {start|stop|restart}`
  151. :Configuration file: /etc/default/celerybeat or /etc/default/celeryd
  152. .. _generic-initd-celerybeat-example:
  153. Example configuration
  154. ~~~~~~~~~~~~~~~~~~~~~
  155. This is an example configuration for a Python project:
  156. `/etc/default/celerybeat`:
  157. .. code-block:: bash
  158. # Absolute or relative path to the 'celery' command:
  159. CELERY_BIN="/usr/local/bin/celery"
  160. #CELERY_BIN="/virtualenvs/def/bin/celery"
  161. # App instance to use
  162. # comment out this line if you don't use an app
  163. CELERY_APP="proj"
  164. # or fully qualified:
  165. #CELERY_APP="proj.tasks:app"
  166. # Where to chdir at start.
  167. CELERYBEAT_CHDIR="/opt/Myproject/"
  168. # Extra arguments to celerybeat
  169. CELERYBEAT_OPTS="--schedule=/var/run/celerybeat-schedule"
  170. .. _generic-initd-celerybeat-django-example:
  171. Example Django configuration
  172. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  173. This is an example configuration for those using `django-celery`
  174. `/etc/default/celerybeat`::
  175. # Where the Django project is.
  176. CELERYBEAT_CHDIR="/opt/Project/"
  177. # Name of the projects settings module.
  178. export DJANGO_SETTINGS_MODULE="settings"
  179. # Path to celery command
  180. CELERY_BIN="/opt/Project/manage.py celery"
  181. # Extra arguments to celerybeat
  182. CELERYBEAT_OPTS="--schedule=/var/run/celerybeat-schedule"
  183. .. _generic-initd-celerybeat-options:
  184. Available options
  185. ~~~~~~~~~~~~~~~~~
  186. * CELERY_APP
  187. App instance to use (value for ``--app`` argument).
  188. * CELERY_BIN
  189. Absolute or relative path to the :program:`celery` program.
  190. Examples:
  191. * :file:`celery``
  192. * :file:`/usr/local/bin/celery`
  193. * :file:`/virtualenvs/proj/bin/celery`
  194. * :file:`/virtualenvs/proj/bin/python -m celery`
  195. * CELERYBEAT_OPTS
  196. Additional arguments to celerybeat, see `celerybeat --help` for a
  197. list.
  198. * CELERYBEAT_PID_FILE
  199. Full path to the PID file. Default is /var/run/celeryd.pid.
  200. * CELERYBEAT_LOG_FILE
  201. Full path to the celeryd log file. Default is /var/log/celeryd.log
  202. * CELERYBEAT_LOG_LEVEL
  203. Log level to use for celeryd. Default is INFO.
  204. * CELERYBEAT_USER
  205. User to run beat as. Default is current user.
  206. * CELERYBEAT_GROUP
  207. Group to run beat 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 celery worker -- --time-limit=300 \
  231. -f /var/log/celeryd.log -l INFO
  232. Run the worker command after `--exec` (without the `--`) to show the
  233. actual resulting output:
  234. .. code-block:: bash
  235. $ /opt/App/release/app/manage.py celery worker --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/