daemonizing.rst 15 KB

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