daemonizing.rst 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. If you're still using the old API, or :pypi:`django-celery`, then you
  107. can omit this setting.
  108. * ``CELERY_BIN``
  109. Absolute or relative path to the :program:`celery` program.
  110. Examples:
  111. * :file:`celery`
  112. * :file:`/usr/local/bin/celery`
  113. * :file:`/virtualenvs/proj/bin/celery`
  114. * :file:`/virtualenvs/proj/bin/python -m celery`
  115. * ``CELERYD_NODES``
  116. List of node names to start (separated by space).
  117. * ``CELERYD_OPTS``
  118. Additional command-line arguments for the worker, see
  119. `celery worker --help` for a list. This also supports the extended
  120. syntax used by `multi` to configure settings for individual nodes.
  121. See `celery multi --help` for some multi-node configuration examples.
  122. * ``CELERYD_CHDIR``
  123. Path to change directory to at start. Default is to stay in the current
  124. directory.
  125. * ``CELERYD_PID_FILE``
  126. Full path to the PID file. Default is /var/run/celery/%n.pid
  127. * ``CELERYD_LOG_FILE``
  128. Full path to the worker log file. Default is /var/log/celery/%n%I.log
  129. **Note**: Using `%I` is important when using the prefork pool as having
  130. multiple processes share the same log file will lead to race conditions.
  131. * ``CELERYD_LOG_LEVEL``
  132. Worker log level. Default is INFO.
  133. * ``CELERYD_USER``
  134. User to run the worker as. Default is current user.
  135. * ``CELERYD_GROUP``
  136. Group to run worker as. Default is current user.
  137. * ``CELERY_CREATE_DIRS``
  138. Always create directories (log directory and pid file directory).
  139. Default is to only create directories when no custom logfile/pidfile set.
  140. * ``CELERY_CREATE_RUNDIR``
  141. Always create pidfile directory. By default only enabled when no custom
  142. pidfile location set.
  143. * ``CELERY_CREATE_LOGDIR``
  144. Always create logfile directory. By default only enable when no custom
  145. logfile location set.
  146. .. _generic-initd-celerybeat:
  147. Init-script: ``celerybeat``
  148. ----------------------------------------------------------------------
  149. :Usage: `/etc/init.d/celerybeat {start|stop|restart}`
  150. :Configuration file: :file:`/etc/default/celerybeat` or
  151. :file:`/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/celery/celerybeat-schedule"
  170. .. _generic-initd-celerybeat-django-example:
  171. Example Django configuration
  172. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  173. You should use the same template as above, but make sure the
  174. ``DJANGO_SETTINGS_MODULE`` variable is set (and exported), and that
  175. ``CELERYD_CHDIR`` is set to the projects directory:
  176. .. code-block:: bash
  177. export DJANGO_SETTINGS_MODULE="settings"
  178. CELERYD_CHDIR="/opt/MyProject"
  179. .. _generic-initd-celerybeat-options:
  180. Available options
  181. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  182. * ``CELERY_APP``
  183. App instance to use (value for :option:`--app <celery --app>` argument).
  184. * ``CELERYBEAT_OPTS``
  185. Additional arguments to :program:`celery beat`, see
  186. :command:`celery beat --help` for a list of available options.
  187. * ``CELERYBEAT_PID_FILE``
  188. Full path to the PID file. Default is :file:`/var/run/celeryd.pid`.
  189. * ``CELERYBEAT_LOG_FILE``
  190. Full path to the log file. Default is :file:`/var/log/celeryd.log`.
  191. * ``CELERYBEAT_LOG_LEVEL``
  192. Log level to use. Default is ``INFO``.
  193. * ``CELERYBEAT_USER``
  194. User to run beat as. Default is the current user.
  195. * ``CELERYBEAT_GROUP``
  196. Group to run beat as. Default is the current user.
  197. * ``CELERY_CREATE_DIRS``
  198. Always create directories (log directory and pid file directory).
  199. Default is to only create directories when no custom logfile/pidfile set.
  200. * ``CELERY_CREATE_RUNDIR``
  201. Always create pidfile directory. By default only enabled when no custom
  202. pidfile location set.
  203. * ``CELERY_CREATE_LOGDIR``
  204. Always create logfile directory. By default only enable when no custom
  205. logfile location set.
  206. .. _generic-initd-troubleshooting:
  207. Troubleshooting
  208. ----------------------------------------------------------------------
  209. If you can't get the init-scripts to work, you should try running
  210. them in *verbose mode*:
  211. .. code-block:: console
  212. # sh -x /etc/init.d/celeryd start
  213. This can reveal hints as to why the service won't start.
  214. If the worker starts with *"OK"* but exits almost immediately afterwards
  215. and there's no evidence in the log file, then there's probably an error
  216. but as the daemons standard outputs are already closed you'll
  217. not be able to see them anywhere. For this situation you can use
  218. the :envvar:`C_FAKEFORK` environment variable to skip the
  219. daemonization step:
  220. .. code-block:: console
  221. # C_FAKEFORK=1 sh -x /etc/init.d/celeryd start
  222. and now you should be able to see the errors.
  223. Commonly such errors are caused by insufficient permissions
  224. to read from, or write to a file, and also by syntax errors
  225. in configuration modules, user modules, third-party libraries,
  226. or even from Celery itself (if you've found a bug you
  227. should :ref:`report it <reporting-bugs>`).
  228. .. _daemon-systemd-generic:
  229. Usage ``systemd``
  230. ======================================================================
  231. * `extra/systemd/`_
  232. .. _`extra/systemd/`:
  233. https://github.com/celery/celery/tree/3.1/extra/systemd/
  234. .. _generic-systemd-celery:
  235. :Usage: `systemctl {start|stop|restart|status} celery.service`
  236. :Configuration file: /etc/conf.d/celery
  237. Service file: celery.service
  238. ----------------------------------------------------------------------
  239. This is an example systemd file:
  240. :file:`/etc/systemd/system/celery.service`:
  241. .. code-block:: bash
  242. [Unit]
  243. Description=Celery Service
  244. After=network.target
  245. [Service]
  246. Type=forking
  247. User=celery
  248. Group=celery
  249. EnvironmentFile=-/etc/conf.d/celery
  250. WorkingDirectory=/opt/celery
  251. ExecStart=/bin/sh '${CELERY_BIN} multi start $CELERYD_NODES \
  252. -A $CELERY_APP --logfile=${CELERYD_LOG_FILE} \
  253. --pidfile=${CELERYD_PID_FILE} $CELERYD_OPTS'
  254. ExecStop=/bin/sh '${CELERY_BIN} multi stopwait $CELERYD_NODES \
  255. --pidfile=${CELERYD_PID_FILE}'
  256. ExecReload=/bin/sh '${CELERY_BIN} multi restart $CELERYD_NODES \
  257. -A $CELERY_APP --pidfile=${CELERYD_PID_FILE} --logfile=${CELERYD_LOG_FILE} \
  258. --loglevel="${CELERYD_LOG_LEVEL}" $CELERYD_OPTS'
  259. [Install]
  260. WantedBy=multi-user.target
  261. Once you've put that file in :file:`/etc/systemd/system`, you should run
  262. :command:`systemctl daemon-reload` in order that Systemd acknowledges that file.
  263. You should also run that command each time you modify it.
  264. To configure user, group, :command:`chdir` change settings:
  265. ``User``, ``Group``, and ``WorkingDirectory`` defined in
  266. :file:`/etc/systemd/system/celery.service`.
  267. You can also use systemd-tmpfiles in order to create working directories (for logs and pid).
  268. :file: `/etc/tmpfiles.d/celery.conf`
  269. .. code-block:: bash
  270. d /var/run/celery 0755 celery celery -
  271. d /var/log/celery 0755 celery celery -
  272. .. _generic-systemd-celery-example:
  273. Example configuration
  274. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  275. This is an example configuration for a Python project:
  276. :file:`/etc/conf.d/celery`:
  277. .. code-block:: bash
  278. # Name of nodes to start
  279. # here we have a single node
  280. CELERYD_NODES="w1"
  281. # or we could have three nodes:
  282. #CELERYD_NODES="w1 w2 w3"
  283. # Absolute or relative path to the 'celery' command:
  284. CELERY_BIN="/usr/local/bin/celery"
  285. #CELERY_BIN="/virtualenvs/def/bin/celery"
  286. # How to call manage.py
  287. CELERYD_MULTI="multi"
  288. # Extra command-line arguments to the worker
  289. CELERYD_OPTS="--time-limit=300 --concurrency=8"
  290. # - %n will be replaced with the first part of the nodename.
  291. # - %I will be replaced with the current child process index
  292. # and is important when using the prefork pool to avoid race conditions.
  293. CELERYD_LOG_FILE="/var/log/celery/%n%I.log"
  294. CELERYD_PID_FILE="/var/run/celery/%n.pid"
  295. .. _generic-systemd-celeryd-django-example:
  296. Example Django configuration
  297. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  298. This is an example configuration for those using :pypi:`django-celery`:
  299. .. code-block:: bash
  300. # Name of nodes to start
  301. # here we have a single node
  302. CELERYD_NODES="w1"
  303. # or we could have three nodes:
  304. #CELERYD_NODES="w1 w2 w3"
  305. # Absolute path to "manage.py"
  306. CELERY_BIN="/opt/Myproject/manage.py"
  307. # How to call manage.py
  308. CELERYD_MULTI="celery multi"
  309. # Extra command-line arguments to the worker
  310. CELERYD_OPTS="--time-limit=300 --concurrency=8"
  311. # - %n will be replaced with the first part of the nodename.
  312. # - %I will be replaced with the current child process index
  313. CELERYD_LOG_FILE="/var/log/celery/%n%I.log"
  314. CELERYD_PID_FILE="/var/run/celery/%n.pid"
  315. To add an environment variable such as :envvar:`DJANGO_SETTINGS_MODULE`
  316. use the Environment in :file:`celery.service`.
  317. Running the worker with superuser privileges (root)
  318. ======================================================================
  319. Running the worker with superuser privileges is a very dangerous practice.
  320. There should always be a workaround to avoid running as root. Celery may
  321. run arbitrary code in messages serialized with pickle - this is dangerous,
  322. especially when run as root.
  323. By default Celery won't run workers as root. The associated error
  324. message may not be visible in the logs but may be seen if :envvar:`C_FAKEFORK`
  325. is used.
  326. To force Celery to run workers as root use :envvar:`C_FORCE_ROOT`.
  327. When running as root without :envvar:`C_FORCE_ROOT` the worker will
  328. appear to start with *"OK"* but exit immediately after with no apparent
  329. errors. This problem may appear when running the project in a new development
  330. or production environment (inadvertently) as root.
  331. .. _daemon-supervisord:
  332. :pypi:`supervisor`
  333. ======================================================================
  334. * `extra/supervisord/`_
  335. .. _`extra/supervisord/`:
  336. https://github.com/celery/celery/tree/master/extra/supervisord/
  337. .. _daemon-launchd:
  338. ``launchd`` (macOS)
  339. ======================================================================
  340. * `extra/macOS`_
  341. .. _`extra/macOS`:
  342. https://github.com/celery/celery/tree/master/extra/macOS/