daemonizing.rst 11 KB

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