daemonizing.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. # Names of nodes to start
  33. # most will only start one node:
  34. CELERYD_NODES="worker1"
  35. # but you can also start multiple and configure settings
  36. # for each in CELERYD_OPTS (see `celery multi --help` for examples).
  37. CELERYD_NODES="worker1 worker2 worker3"
  38. # Absolute or relative path to the 'celery' command:
  39. CELERY_BIN="/usr/local/bin/celery"
  40. #CELERY_BIN="/virtualenvs/def/bin/celery"
  41. # App instance to use
  42. # comment out this line if you don't use an app
  43. CELERY_APP="proj"
  44. # or fully qualified:
  45. #CELERY_APP="proj.tasks:app"
  46. # Where to chdir at start.
  47. CELERYD_CHDIR="/opt/Myproject/"
  48. # Extra command-line arguments to the worker
  49. CELERYD_OPTS="--time-limit=300 --concurrency=8"
  50. # %N will be replaced with the first part of the nodename.
  51. CELERYD_LOG_FILE="/var/log/celery/%N.log"
  52. CELERYD_PID_FILE="/var/run/celery/%N.pid"
  53. # Workers should run as an unprivileged user.
  54. # You need to create this user manually (or you can choose
  55. # a user/group combination that already exists, e.g. nobody).
  56. CELERYD_USER="celery"
  57. CELERYD_GROUP="celery"
  58. # If enabled pid and log directories will be created if missing,
  59. # and owned by the userid/group configured.
  60. CELERY_CREATE_DIRS=1
  61. .. _generic-initd-celeryd-django-example:
  62. Example Django configuration
  63. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  64. You should use the same template as above, but make sure the
  65. ``DJANGO_SETTINGS_MODULE`` variable is set (and exported), and that
  66. ``CELERYD_CHDIR`` is set to the projects directory:
  67. .. code-block:: bash
  68. export DJANGO_SETTINGS_MODULE="settings"
  69. CELERYD_CHDIR="/opt/MyProject"
  70. .. _generic-initd-celeryd-options:
  71. Available options
  72. ~~~~~~~~~~~~~~~~~~
  73. * CELERY_APP
  74. App instance to use (value for ``--app`` argument).
  75. If you're still using the old API, or django-celery, then you
  76. can omit this setting.
  77. * CELERY_BIN
  78. Absolute or relative path to the :program:`celery` program.
  79. Examples:
  80. * :file:`celery`
  81. * :file:`/usr/local/bin/celery`
  82. * :file:`/virtualenvs/proj/bin/celery`
  83. * :file:`/virtualenvs/proj/bin/python -m celery`
  84. * CELERYD_NODES
  85. List of node names to start (separated by space).
  86. * CELERYD_OPTS
  87. Additional command-line arguments for the worker, see
  88. `celery worker --help` for a list. This also supports the extended
  89. syntax used by `multi` to configure settings for individual nodes.
  90. See `celery multi --help` for some multi-node configuration examples.
  91. * CELERYD_CHDIR
  92. Path to change directory to at start. Default is to stay in the current
  93. directory.
  94. * CELERYD_PID_FILE
  95. Full path to the PID file. Default is /var/run/celery/%N.pid
  96. * CELERYD_LOG_FILE
  97. Full path to the worker log file. Default is /var/log/celery/%N.log
  98. * CELERYD_LOG_LEVEL
  99. Worker log level. Default is INFO.
  100. * CELERYD_USER
  101. User to run the worker as. Default is current user.
  102. * CELERYD_GROUP
  103. Group to run worker as. Default is current user.
  104. * CELERY_CREATE_DIRS
  105. Always create directories (log directory and pid file directory).
  106. Default is to only create directories when no custom logfile/pidfile set.
  107. * CELERY_CREATE_RUNDIR
  108. Always create pidfile directory. By default only enabled when no custom
  109. pidfile location set.
  110. * CELERY_CREATE_LOGDIR
  111. Always create logfile directory. By default only enable when no custom
  112. logfile location set.
  113. .. _generic-initd-celerybeat:
  114. Init script: celerybeat
  115. -----------------------
  116. :Usage: `/etc/init.d/celerybeat {start|stop|restart}`
  117. :Configuration file: /etc/default/celerybeat or /etc/default/celeryd
  118. .. _generic-initd-celerybeat-example:
  119. Example configuration
  120. ~~~~~~~~~~~~~~~~~~~~~
  121. This is an example configuration for a Python project:
  122. `/etc/default/celerybeat`:
  123. .. code-block:: bash
  124. # Absolute or relative path to the 'celery' command:
  125. CELERY_BIN="/usr/local/bin/celery"
  126. #CELERY_BIN="/virtualenvs/def/bin/celery"
  127. # App instance to use
  128. # comment out this line if you don't use an app
  129. CELERY_APP="proj"
  130. # or fully qualified:
  131. #CELERY_APP="proj.tasks:app"
  132. # Where to chdir at start.
  133. CELERYBEAT_CHDIR="/opt/Myproject/"
  134. # Extra arguments to celerybeat
  135. CELERYBEAT_OPTS="--schedule=/var/run/celerybeat-schedule"
  136. .. _generic-initd-celerybeat-django-example:
  137. Example Django configuration
  138. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  139. You should use the same template as above, but make sure the
  140. ``DJANGO_SETTINGS_MODULE`` variable is set (and exported), and that
  141. ``CELERYD_CHDIR`` is set to the projects directory:
  142. .. code-block:: bash
  143. export DJANGO_SETTINGS_MODULE="settings"
  144. CELERYD_CHDIR="/opt/MyProject"
  145. .. _generic-initd-celerybeat-options:
  146. Available options
  147. ~~~~~~~~~~~~~~~~~
  148. * CELERY_APP
  149. App instance to use (value for ``--app`` argument).
  150. * CELERYBEAT_OPTS
  151. Additional arguments to celerybeat, see `celerybeat --help` for a
  152. list.
  153. * CELERYBEAT_PID_FILE
  154. Full path to the PID file. Default is /var/run/celeryd.pid.
  155. * CELERYBEAT_LOG_FILE
  156. Full path to the celeryd log file. Default is /var/log/celeryd.log
  157. * CELERYBEAT_LOG_LEVEL
  158. Log level to use for celeryd. Default is INFO.
  159. * CELERYBEAT_USER
  160. User to run beat as. Default is current user.
  161. * CELERYBEAT_GROUP
  162. Group to run beat as. Default is current user.
  163. * CELERY_CREATE_DIRS
  164. Always create directories (log directory and pid file directory).
  165. Default is to only create directories when no custom logfile/pidfile set.
  166. * CELERY_CREATE_RUNDIR
  167. Always create pidfile directory. By default only enabled when no custom
  168. pidfile location set.
  169. * CELERY_CREATE_LOGDIR
  170. Always create logfile directory. By default only enable when no custom
  171. logfile location set.
  172. .. _daemon-systemd-generic:
  173. Usage systemd
  174. =============
  175. .. _generic-systemd-celery:
  176. Service file: celery.service
  177. ----------------------------
  178. :Usage: `systemctl {start|stop|restart|status} celery.service`
  179. :Configuration file: /etc/conf.d/celery
  180. To create a temporary folders for the log and pid files change user and group in
  181. /usr/lib/tmpfiles.d/celery.conf.
  182. To configure user, group, chdir change settings User, Group and WorkingDirectory defines
  183. in /usr/lib/systemd/system/celery.service.
  184. .. _generic-systemd-celery-example:
  185. Example configuration
  186. ~~~~~~~~~~~~~~~~~~~~~
  187. This is an example configuration for a Python project:
  188. :file:`/etc/conf.d/celery`:
  189. .. code-block:: bash
  190. # Name of nodes to start
  191. # here we have a single node
  192. CELERYD_NODES="w1"
  193. # or we could have three nodes:
  194. #CELERYD_NODES="w1 w2 w3"
  195. # Absolute or relative path to the 'celery' command:
  196. CELERY_BIN="/usr/local/bin/celery"
  197. #CELERY_BIN="/virtualenvs/def/bin/celery"
  198. # How to call manage.py
  199. CELERYD_MULTI="multi"
  200. # Extra command-line arguments to the worker
  201. CELERYD_OPTS="--time-limit=300 --concurrency=8"
  202. # %N will be replaced with the first part of the nodename.
  203. CELERYD_LOG_FILE="/var/log/celery/%N.log"
  204. CELERYD_PID_FILE="/var/run/celery/%N.pid"
  205. .. _generic-systemd-celeryd-django-example:
  206. Example Django configuration
  207. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  208. This is an example configuration for those using `django-celery`:
  209. .. code-block:: bash
  210. # Name of nodes to start
  211. # here we have a single node
  212. CELERYD_NODES="w1"
  213. # or we could have three nodes:
  214. #CELERYD_NODES="w1 w2 w3"
  215. # Absolute path to "manage.py"
  216. CELERY_BIN="/opt/Myproject/manage.py"
  217. # How to call manage.py
  218. CELERYD_MULTI="celery multi"
  219. # Extra command-line arguments to the worker
  220. CELERYD_OPTS="--time-limit=300 --concurrency=8"
  221. # %N will be replaced with the first part of the nodename.
  222. CELERYD_LOG_FILE="/var/log/celery/%N.log"
  223. CELERYD_PID_FILE="/var/run/celery/%N.pid"
  224. To add an environment variable such as DJANGO_SETTINGS_MODULE use the
  225. Environment in celery.service.
  226. .. _generic-initd-troubleshooting:
  227. Troubleshooting
  228. ---------------
  229. If you can't get the init scripts to work, you should try running
  230. them in *verbose mode*::
  231. $ sh -x /etc/init.d/celeryd start
  232. This can reveal hints as to why the service won't start.
  233. Also you will see the commands generated, so you can try to run the celeryd
  234. command manually to read the resulting error output.
  235. For example my `sh -x` output does this:
  236. .. code-block:: bash
  237. ++ start-stop-daemon --start --chdir /opt/App/release/app --quiet \
  238. --oknodo --background --make-pidfile --pidfile /var/run/celeryd.pid \
  239. --exec /opt/App/release/app/manage.py celery worker -- --time-limit=300 \
  240. -f /var/log/celeryd.log -l INFO
  241. Run the worker command after `--exec` (without the `--`) to show the
  242. actual resulting output:
  243. .. code-block:: bash
  244. $ /opt/App/release/app/manage.py celery worker --time-limit=300 \
  245. -f /var/log/celeryd.log -l INFO
  246. .. _daemon-supervisord:
  247. `supervisord`_
  248. ==============
  249. * `extra/supervisord/`_
  250. .. _`extra/supervisord/`:
  251. http://github.com/celery/celery/tree/3.0/extra/supervisord/
  252. .. _`supervisord`: http://supervisord.org/
  253. .. _daemon-launchd:
  254. launchd (OS X)
  255. ==============
  256. * `extra/mac/`_
  257. .. _`extra/mac/`:
  258. http://github.com/celery/celery/tree/3.0/extra/mac/
  259. .. _daemon-windows:
  260. Windows
  261. =======
  262. See this excellent external tutorial:
  263. http://www.calazan.com/windows-tip-run-applications-in-the-background-using-task-scheduler/
  264. CentOS
  265. ======
  266. In CentOS we can take advantage of built-in service helpers, such as the
  267. pid-based status checker function in ``/etc/init.d/functions``.
  268. See the sample script in http://github.com/celery/celery/tree/3.0/extra/centos/.