daemonizing.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. .. _daemonizing:
  2. ======================================================================
  3. Daemonization
  4. ======================================================================
  5. .. contents::
  6. :local:
  7. .. _daemon-generic:
  8. Generic init-scripts
  9. ======================================================================
  10. See the `extra/generic-init.d/`_ directory Celery distribution.
  11. This directory contains generic bash init-scripts for the
  12. :program:`celery worker` program,
  13. these should run on Linux, FreeBSD, OpenBSD, and other Unix-like platforms.
  14. .. _`extra/generic-init.d/`:
  15. https://github.com/celery/celery/tree/3.1/extra/generic-init.d/
  16. .. _generic-initd-celeryd:
  17. Init-script: ``celeryd``
  18. ----------------------------------------------------------------------
  19. :Usage: `/etc/init.d/celeryd {start|stop|restart|status}`
  20. :Configuration file: :file:`/etc/default/celeryd`
  21. To configure this script to run the worker properly you probably need to at least
  22. tell it where to change
  23. directory to when it starts (to find the module containing your app, or your
  24. configuration module).
  25. The daemonization script is configured by the file :file:`/etc/default/celeryd`.
  26. This is a shell (:command:`sh`) script where you can add environment variables like
  27. the configuration options below. To add real environment variables affecting
  28. the worker you must also export them (e.g., :command:`export DISPLAY=":0"`)
  29. .. Admonition:: Superuser privileges required
  30. The init-scripts can only be used by root,
  31. and the shell configuration file must also be owned by root.
  32. Unprivileged users don't need to use the init-script,
  33. instead they can use the :program:`celery multi` utility (or
  34. :program:`celery worker --detach`):
  35. .. code-block:: console
  36. $ celery multi start worker1 \
  37. -A proj \
  38. --pidfile="$HOME/run/celery/%n.pid" \
  39. --logfile="$HOME/log/celery/%n%I.log"
  40. $ celery multi restart worker1 \
  41. -A proj \
  42. --logfile="$HOME/log/celery/%n%I.log" \
  43. --pidfile="$HOME/run/celery/%n.pid
  44. $ celery multi stopwait worker1 --pidfile="$HOME/run/celery/%n.pid"
  45. .. _generic-initd-celeryd-example:
  46. Example configuration
  47. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  48. This is an example configuration for a Python project.
  49. :file:`/etc/default/celeryd`:
  50. .. code-block:: bash
  51. # Names of nodes to start
  52. # most people will only start one node:
  53. CELERYD_NODES="worker1"
  54. # but you can also start multiple and configure settings
  55. # for each in CELERYD_OPTS
  56. #CELERYD_NODES="worker1 worker2 worker3"
  57. # alternatively, you can specify the number of nodes to start:
  58. #CELERYD_NODES=10
  59. # Absolute or relative path to the 'celery' command:
  60. CELERY_BIN="/usr/local/bin/celery"
  61. #CELERY_BIN="/virtualenvs/def/bin/celery"
  62. # App instance to use
  63. # comment out this line if you don't use an app
  64. CELERY_APP="proj"
  65. # or fully qualified:
  66. #CELERY_APP="proj.tasks:app"
  67. # Where to chdir at start.
  68. CELERYD_CHDIR="/opt/Myproject/"
  69. # Extra command-line arguments to the worker
  70. CELERYD_OPTS="--time-limit=300 --concurrency=8"
  71. # Configure node-specific settings by appending node name to arguments:
  72. #CELERYD_OPTS="--time-limit=300 -c 8 -c:worker2 4 -c:worker3 2 -Ofair:worker1"
  73. # Set logging level to DEBUG
  74. #CELERYD_LOG_LEVEL="DEBUG"
  75. # %n will be replaced with the first part of the nodename.
  76. CELERYD_LOG_FILE="/var/log/celery/%n%I.log"
  77. CELERYD_PID_FILE="/var/run/celery/%n.pid"
  78. # Workers should run as an unprivileged user.
  79. # You need to create this user manually (or you can choose
  80. # a user/group combination that already exists (e.g., nobody).
  81. CELERYD_USER="celery"
  82. CELERYD_GROUP="celery"
  83. # If enabled pid and log directories will be created if missing,
  84. # and owned by the userid/group configured.
  85. CELERY_CREATE_DIRS=1
  86. Using a login shell
  87. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  88. You can inherit the environment of the ``CELERYD_USER`` by using a login
  89. shell:
  90. .. code-block:: bash
  91. CELERYD_SU_ARGS="-l"
  92. Note that this isn't recommended, and that you should only use this option
  93. when absolutely necessary.
  94. .. _generic-initd-celeryd-django-example:
  95. Example Django configuration
  96. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  97. Django users now uses the exact same template as above,
  98. but make sure that the module that defines your Celery app instance
  99. also sets a default value for :envvar:`DJANGO_SETTINGS_MODULE`
  100. as shown in the example Django project in :ref:`django-first-steps`.
  101. .. _generic-initd-celeryd-options:
  102. Available options
  103. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  104. * ``CELERY_APP``
  105. App instance to use (value for :option:`--app <celery --app>` argument).
  106. * ``CELERY_BIN``
  107. Absolute or relative path to the :program:`celery` program.
  108. Examples:
  109. * :file:`celery`
  110. * :file:`/usr/local/bin/celery`
  111. * :file:`/virtualenvs/proj/bin/celery`
  112. * :file:`/virtualenvs/proj/bin/python -m celery`
  113. * ``CELERYD_NODES``
  114. List of node names to start (separated by space).
  115. * ``CELERYD_OPTS``
  116. Additional command-line arguments for the worker, see
  117. `celery worker --help` for a list. This also supports the extended
  118. syntax used by `multi` to configure settings for individual nodes.
  119. See `celery multi --help` for some multi-node configuration examples.
  120. * ``CELERYD_CHDIR``
  121. Path to change directory to at start. Default is to stay in the current
  122. directory.
  123. * ``CELERYD_PID_FILE``
  124. Full path to the PID file. Default is /var/run/celery/%n.pid
  125. * ``CELERYD_LOG_FILE``
  126. Full path to the worker log file. Default is /var/log/celery/%n%I.log
  127. **Note**: Using `%I` is important when using the prefork pool as having
  128. multiple processes share the same log file will lead to race conditions.
  129. * ``CELERYD_LOG_LEVEL``
  130. Worker log level. Default is INFO.
  131. * ``CELERYD_USER``
  132. User to run the worker as. Default is current user.
  133. * ``CELERYD_GROUP``
  134. Group to run worker as. Default is current user.
  135. * ``CELERY_CREATE_DIRS``
  136. Always create directories (log directory and pid file directory).
  137. Default is to only create directories when no custom logfile/pidfile set.
  138. * ``CELERY_CREATE_RUNDIR``
  139. Always create pidfile directory. By default only enabled when no custom
  140. pidfile location set.
  141. * ``CELERY_CREATE_LOGDIR``
  142. Always create logfile directory. By default only enable when no custom
  143. logfile location set.
  144. .. _generic-initd-celerybeat:
  145. Init-script: ``celerybeat``
  146. ----------------------------------------------------------------------
  147. :Usage: `/etc/init.d/celerybeat {start|stop|restart}`
  148. :Configuration file: :file:`/etc/default/celerybeat` or
  149. :file:`/etc/default/celeryd`.
  150. .. _generic-initd-celerybeat-example:
  151. Example configuration
  152. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  153. This is an example configuration for a Python project:
  154. `/etc/default/celerybeat`:
  155. .. code-block:: bash
  156. # Absolute or relative path to the 'celery' command:
  157. CELERY_BIN="/usr/local/bin/celery"
  158. #CELERY_BIN="/virtualenvs/def/bin/celery"
  159. # App instance to use
  160. # comment out this line if you don't use an app
  161. CELERY_APP="proj"
  162. # or fully qualified:
  163. #CELERY_APP="proj.tasks:app"
  164. # Where to chdir at start.
  165. CELERYBEAT_CHDIR="/opt/Myproject/"
  166. # Extra arguments to celerybeat
  167. CELERYBEAT_OPTS="--schedule=/var/run/celery/celerybeat-schedule"
  168. .. _generic-initd-celerybeat-django-example:
  169. Example Django configuration
  170. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  171. You should use the same template as above, but make sure the
  172. ``DJANGO_SETTINGS_MODULE`` variable is set (and exported), and that
  173. ``CELERYD_CHDIR`` is set to the projects directory:
  174. .. code-block:: bash
  175. export DJANGO_SETTINGS_MODULE="settings"
  176. CELERYD_CHDIR="/opt/MyProject"
  177. .. _generic-initd-celerybeat-options:
  178. Available options
  179. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  180. * ``CELERY_APP``
  181. App instance to use (value for :option:`--app <celery --app>` argument).
  182. * ``CELERYBEAT_OPTS``
  183. Additional arguments to :program:`celery beat`, see
  184. :command:`celery beat --help` for a list of available options.
  185. * ``CELERYBEAT_PID_FILE``
  186. Full path to the PID file. Default is :file:`/var/run/celeryd.pid`.
  187. * ``CELERYBEAT_LOG_FILE``
  188. Full path to the log file. Default is :file:`/var/log/celeryd.log`.
  189. * ``CELERYBEAT_LOG_LEVEL``
  190. Log level to use. Default is ``INFO``.
  191. * ``CELERYBEAT_USER``
  192. User to run beat as. Default is the current user.
  193. * ``CELERYBEAT_GROUP``
  194. Group to run beat as. Default is the current user.
  195. * ``CELERY_CREATE_DIRS``
  196. Always create directories (log directory and pid file directory).
  197. Default is to only create directories when no custom logfile/pidfile set.
  198. * ``CELERY_CREATE_RUNDIR``
  199. Always create pidfile directory. By default only enabled when no custom
  200. pidfile location set.
  201. * ``CELERY_CREATE_LOGDIR``
  202. Always create logfile directory. By default only enable when no custom
  203. logfile location set.
  204. .. _generic-initd-troubleshooting:
  205. Troubleshooting
  206. ----------------------------------------------------------------------
  207. If you can't get the init-scripts to work, you should try running
  208. them in *verbose mode*:
  209. .. code-block:: console
  210. # sh -x /etc/init.d/celeryd start
  211. This can reveal hints as to why the service won't start.
  212. If the worker starts with *"OK"* but exits almost immediately afterwards
  213. and there's no evidence in the log file, then there's probably an error
  214. but as the daemons standard outputs are already closed you'll
  215. not be able to see them anywhere. For this situation you can use
  216. the :envvar:`C_FAKEFORK` environment variable to skip the
  217. daemonization step:
  218. .. code-block:: console
  219. # C_FAKEFORK=1 sh -x /etc/init.d/celeryd start
  220. and now you should be able to see the errors.
  221. Commonly such errors are caused by insufficient permissions
  222. to read from, or write to a file, and also by syntax errors
  223. in configuration modules, user modules, third-party libraries,
  224. or even from Celery itself (if you've found a bug you
  225. should :ref:`report it <reporting-bugs>`).
  226. .. _daemon-systemd-generic:
  227. Usage ``systemd``
  228. ======================================================================
  229. * `extra/systemd/`_
  230. .. _`extra/systemd/`:
  231. https://github.com/celery/celery/tree/3.1/extra/systemd/
  232. .. _generic-systemd-celery:
  233. :Usage: `systemctl {start|stop|restart|status} celery.service`
  234. :Configuration file: /etc/conf.d/celery
  235. Service file: celery.service
  236. ----------------------------------------------------------------------
  237. This is an example systemd file:
  238. :file:`/etc/systemd/system/celery.service`:
  239. .. code-block:: bash
  240. [Unit]
  241. Description=Celery Service
  242. After=network.target
  243. [Service]
  244. Type=forking
  245. User=celery
  246. Group=celery
  247. EnvironmentFile=-/etc/conf.d/celery
  248. WorkingDirectory=/opt/celery
  249. ExecStart=/bin/sh -c '${CELERY_BIN} multi start ${CELERYD_NODES} \
  250. -A ${CELERY_APP} --pidfile=${CELERYD_PID_FILE} \
  251. --logfile=${CELERYD_LOG_FILE} --loglevel=${CELERYD_LOG_LEVEL} ${CELERYD_OPTS}'
  252. ExecStop=/bin/sh -c '${CELERY_BIN} multi stopwait ${CELERYD_NODES} \
  253. --pidfile=${CELERYD_PID_FILE}'
  254. ExecReload=/bin/sh -c '${CELERY_BIN} multi restart ${CELERYD_NODES} \
  255. -A ${CELERY_APP} --pidfile=${CELERYD_PID_FILE} \
  256. --logfile=${CELERYD_LOG_FILE} --loglevel=${CELERYD_LOG_LEVEL} ${CELERYD_OPTS}'
  257. [Install]
  258. WantedBy=multi-user.target
  259. Once you've put that file in :file:`/etc/systemd/system`, you should run
  260. :command:`systemctl daemon-reload` in order that Systemd acknowledges that file.
  261. You should also run that command each time you modify it.
  262. To configure user, group, :command:`chdir` change settings:
  263. ``User``, ``Group``, and ``WorkingDirectory`` defined in
  264. :file:`/etc/systemd/system/celery.service`.
  265. You can also use systemd-tmpfiles in order to create working directories (for logs and pid).
  266. :file: `/etc/tmpfiles.d/celery.conf`
  267. .. code-block:: bash
  268. d /var/run/celery 0755 celery celery -
  269. d /var/log/celery 0755 celery celery -
  270. .. _generic-systemd-celery-example:
  271. Example configuration
  272. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  273. This is an example configuration for a Python project:
  274. :file:`/etc/conf.d/celery`:
  275. .. code-block:: bash
  276. # Name of nodes to start
  277. # here we have a single node
  278. CELERYD_NODES="w1"
  279. # or we could have three nodes:
  280. #CELERYD_NODES="w1 w2 w3"
  281. # Absolute or relative path to the 'celery' command:
  282. CELERY_BIN="/usr/local/bin/celery"
  283. #CELERY_BIN="/virtualenvs/def/bin/celery"
  284. # App instance to use
  285. # comment out this line if you don't use an app
  286. CELERY_APP="proj"
  287. # or fully qualified:
  288. #CELERY_APP="proj.tasks:app"
  289. # How to call manage.py
  290. CELERYD_MULTI="multi"
  291. # Extra command-line arguments to the worker
  292. CELERYD_OPTS="--time-limit=300 --concurrency=8"
  293. # - %n will be replaced with the first part of the nodename.
  294. # - %I will be replaced with the current child process index
  295. # and is important when using the prefork pool to avoid race conditions.
  296. CELERYD_PID_FILE="/var/run/celery/%n.pid"
  297. CELERYD_LOG_FILE="/var/log/celery/%n%I.log"
  298. CELERYD_LOG_LEVEL="INFO"
  299. Running the worker with superuser privileges (root)
  300. ======================================================================
  301. Running the worker with superuser privileges is a very dangerous practice.
  302. There should always be a workaround to avoid running as root. Celery may
  303. run arbitrary code in messages serialized with pickle - this is dangerous,
  304. especially when run as root.
  305. By default Celery won't run workers as root. The associated error
  306. message may not be visible in the logs but may be seen if :envvar:`C_FAKEFORK`
  307. is used.
  308. To force Celery to run workers as root use :envvar:`C_FORCE_ROOT`.
  309. When running as root without :envvar:`C_FORCE_ROOT` the worker will
  310. appear to start with *"OK"* but exit immediately after with no apparent
  311. errors. This problem may appear when running the project in a new development
  312. or production environment (inadvertently) as root.
  313. .. _daemon-supervisord:
  314. :pypi:`supervisor`
  315. ======================================================================
  316. * `extra/supervisord/`_
  317. .. _`extra/supervisord/`:
  318. https://github.com/celery/celery/tree/master/extra/supervisord/
  319. .. _daemon-launchd:
  320. ``launchd`` (macOS)
  321. ======================================================================
  322. * `extra/macOS`_
  323. .. _`extra/macOS`:
  324. https://github.com/celery/celery/tree/master/extra/macOS/