Debian Codegraph @

docs/userguide/daemonizing.rst

raw
1 .. _daemonizing:
2
3 ======================================================================
4 Daemonization
5 ======================================================================
6
7 .. contents::
8 :local:
9
10 Most Linux distributions these days use systemd for managing the lifecycle of system
11 and user services.
12
13 You can check if your Linux distribution uses systemd by typing:
14
15 .. code-block:: console
16
17 $ systemctl --version
18 systemd 249 (v249.9-1.fc35)
19 +PAM +AUDIT +SELINUX -APPARMOR +IMA +SMACK +SECCOMP +GCRYPT +GNUTLS +OPENSSL +ACL +BLKID +CURL +ELFUTILS +FIDO2 +IDN2 -IDN +IPTC +KMOD +LIBCRYPTSETUP +LIBFDISK +PCRE2 +PWQUALITY +P11KIT +QRENCODE +BZIP2 +LZ4 +XZ +ZLIB +ZSTD +XKBCOMMON +UTMP +SYSVINIT default-hierarchy=unified
20
21 If you have output similar to the above, please refer to
22 :ref:`our systemd documentation <daemon-systemd-generic>` for guidance.
23
24 However, the init.d script should still work in those Linux distributions
25 as well since systemd provides the systemd-sysv compatibility layer
26 which generates services automatically from the init.d scripts we provide.
27
28 If you package Celery for multiple Linux distributions
29 and some do not support systemd or to other Unix systems as well,
30 you may want to refer to :ref:`our init.d documentation <daemon-generic>`.
31
32 .. _daemon-generic:
33
34 Generic init-scripts
35 ======================================================================
36
37 See the `extra/generic-init.d/`_ directory Celery distribution.
38
39 This directory contains generic bash init-scripts for the
40 :program:`celery worker` program,
41 these should run on Linux, FreeBSD, OpenBSD, and other Unix-like platforms.
42
43 .. _`extra/generic-init.d/`:
44 https://github.com/celery/celery/tree/main/extra/generic-init.d/
45
46 .. _generic-initd-celeryd:
47
48 Init-script: ``celeryd``
49 ----------------------------------------------------------------------
50
51 :Usage: `/etc/init.d/celeryd {start|stop|restart|status}`
52 :Configuration file: :file:`/etc/default/celeryd`
53
54 To configure this script to run the worker properly you probably need to at least
55 tell it where to change
56 directory to when it starts (to find the module containing your app, or your
57 configuration module).
58
59 The daemonization script is configured by the file :file:`/etc/default/celeryd`.
60 This is a shell (:command:`sh`) script where you can add environment variables like
61 the configuration options below. To add real environment variables affecting
62 the worker you must also export them (e.g., :command:`export DISPLAY=":0"`)
63
64 .. Admonition:: Superuser privileges required
65
66 The init-scripts can only be used by root,
67 and the shell configuration file must also be owned by root.
68
69 Unprivileged users don't need to use the init-script,
70 instead they can use the :program:`celery multi` utility (or
71 :program:`celery worker --detach`):
72
73 .. code-block:: console
74
75 $ celery -A proj multi start worker1 \
76 --pidfile="$HOME/run/celery/%n.pid" \
77 --logfile="$HOME/log/celery/%n%I.log"
78
79 $ celery -A proj multi restart worker1 \
80 --logfile="$HOME/log/celery/%n%I.log" \
81 --pidfile="$HOME/run/celery/%n.pid
82
83 $ celery multi stopwait worker1 --pidfile="$HOME/run/celery/%n.pid"
84
85 .. _generic-initd-celeryd-example:
86
87 Example configuration
88 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
89
90 This is an example configuration for a Python project.
91
92 :file:`/etc/default/celeryd`:
93
94 .. code-block:: bash
95
96 # Names of nodes to start
97 # most people will only start one node:
98 CELERYD_NODES="worker1"
99 # but you can also start multiple and configure settings
100 # for each in CELERYD_OPTS
101 #CELERYD_NODES="worker1 worker2 worker3"
102 # alternatively, you can specify the number of nodes to start:
103 #CELERYD_NODES=10
104
105 # Absolute or relative path to the 'celery' command:
106 CELERY_BIN="/usr/local/bin/celery"
107 #CELERY_BIN="/virtualenvs/def/bin/celery"
108
109 # App instance to use
110 # comment out this line if you don't use an app
111 CELERY_APP="proj"
112 # or fully qualified:
113 #CELERY_APP="proj.tasks:app"
114
115 # Where to chdir at start.
116 CELERYD_CHDIR="/opt/Myproject/"
117
118 # Extra command-line arguments to the worker
119 CELERYD_OPTS="--time-limit=300 --concurrency=8"
120 # Configure node-specific settings by appending node name to arguments:
121 #CELERYD_OPTS="--time-limit=300 -c 8 -c:worker2 4 -c:worker3 2 -Ofair:worker1"
122
123 # Set logging level to DEBUG
124 #CELERYD_LOG_LEVEL="DEBUG"
125
126 # %n will be replaced with the first part of the nodename.
127 CELERYD_LOG_FILE="/var/log/celery/%n%I.log"
128 CELERYD_PID_FILE="/var/run/celery/%n.pid"
129
130 # Workers should run as an unprivileged user.
131 # You need to create this user manually (or you can choose
132 # a user/group combination that already exists (e.g., nobody).
133 CELERYD_USER="celery"
134 CELERYD_GROUP="celery"
135
136 # If enabled pid and log directories will be created if missing,
137 # and owned by the userid/group configured.
138 CELERY_CREATE_DIRS=1
139
140 Using a login shell
141 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
142
143 You can inherit the environment of the ``CELERYD_USER`` by using a login
144 shell:
145
146 .. code-block:: bash
147
148 CELERYD_SU_ARGS="-l"
149
150 Note that this isn't recommended, and that you should only use this option
151 when absolutely necessary.
152
153 .. _generic-initd-celeryd-django-example:
154
155 Example Django configuration
156 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
157
158 Django users now uses the exact same template as above,
159 but make sure that the module that defines your Celery app instance
160 also sets a default value for :envvar:`DJANGO_SETTINGS_MODULE`
161 as shown in the example Django project in :ref:`django-first-steps`.
162
163 .. _generic-initd-celeryd-options:
164
165 Available options
166 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
167
168 * ``CELERY_APP``
169
170 App instance to use (value for :option:`--app <celery --app>` argument).
171
172 * ``CELERY_BIN``
173
174 Absolute or relative path to the :program:`celery` program.
175 Examples:
176
177 * :file:`celery`
178 * :file:`/usr/local/bin/celery`
179 * :file:`/virtualenvs/proj/bin/celery`
180 * :file:`/virtualenvs/proj/bin/python -m celery`
181
182 * ``CELERYD_NODES``
183
184 List of node names to start (separated by space).
185
186 * ``CELERYD_OPTS``
187
188 Additional command-line arguments for the worker, see
189 `celery worker --help` for a list. This also supports the extended
190 syntax used by `multi` to configure settings for individual nodes.
191 See `celery multi --help` for some multi-node configuration examples.
192
193 * ``CELERYD_CHDIR``
194
195 Path to change directory to at start. Default is to stay in the current
196 directory.
197
198 * ``CELERYD_PID_FILE``
199
200 Full path to the PID file. Default is /var/run/celery/%n.pid
201
202 * ``CELERYD_LOG_FILE``
203
204 Full path to the worker log file. Default is /var/log/celery/%n%I.log
205 **Note**: Using `%I` is important when using the prefork pool as having
206 multiple processes share the same log file will lead to race conditions.
207
208 * ``CELERYD_LOG_LEVEL``
209
210 Worker log level. Default is INFO.
211
212 * ``CELERYD_USER``
213
214 User to run the worker as. Default is current user.
215
216 * ``CELERYD_GROUP``
217
218 Group to run worker as. Default is current user.
219
220 * ``CELERY_CREATE_DIRS``
221
222 Always create directories (log directory and pid file directory).
223 Default is to only create directories when no custom logfile/pidfile set.
224
225 * ``CELERY_CREATE_RUNDIR``
226
227 Always create pidfile directory. By default only enabled when no custom
228 pidfile location set.
229
230 * ``CELERY_CREATE_LOGDIR``
231
232 Always create logfile directory. By default only enable when no custom
233 logfile location set.
234
235 .. _generic-initd-celerybeat:
236
237 Init-script: ``celerybeat``
238 ----------------------------------------------------------------------
239 :Usage: `/etc/init.d/celerybeat {start|stop|restart}`
240 :Configuration file: :file:`/etc/default/celerybeat` or
241 :file:`/etc/default/celeryd`.
242
243 .. _generic-initd-celerybeat-example:
244
245 Example configuration
246 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
247
248 This is an example configuration for a Python project:
249
250 `/etc/default/celerybeat`:
251
252 .. code-block:: bash
253
254 # Absolute or relative path to the 'celery' command:
255 CELERY_BIN="/usr/local/bin/celery"
256 #CELERY_BIN="/virtualenvs/def/bin/celery"
257
258 # App instance to use
259 # comment out this line if you don't use an app
260 CELERY_APP="proj"
261 # or fully qualified:
262 #CELERY_APP="proj.tasks:app"
263
264 # Where to chdir at start.
265 CELERYBEAT_CHDIR="/opt/Myproject/"
266
267 # Extra arguments to celerybeat
268 CELERYBEAT_OPTS="--schedule=/var/run/celery/celerybeat-schedule"
269
270 .. _generic-initd-celerybeat-django-example:
271
272 Example Django configuration
273 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
274
275 You should use the same template as above, but make sure the
276 ``DJANGO_SETTINGS_MODULE`` variable is set (and exported), and that
277 ``CELERYD_CHDIR`` is set to the projects directory:
278
279 .. code-block:: bash
280
281 export DJANGO_SETTINGS_MODULE="settings"
282
283 CELERYD_CHDIR="/opt/MyProject"
284 .. _generic-initd-celerybeat-options:
285
286 Available options
287 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
288
289 * ``CELERY_APP``
290
291 App instance to use (value for :option:`--app <celery --app>` argument).
292
293 * ``CELERYBEAT_OPTS``
294
295 Additional arguments to :program:`celery beat`, see
296 :command:`celery beat --help` for a list of available options.
297
298 * ``CELERYBEAT_PID_FILE``
299
300 Full path to the PID file. Default is :file:`/var/run/celeryd.pid`.
301
302 * ``CELERYBEAT_LOG_FILE``
303
304 Full path to the log file. Default is :file:`/var/log/celeryd.log`.
305
306 * ``CELERYBEAT_LOG_LEVEL``
307
308 Log level to use. Default is ``INFO``.
309
310 * ``CELERYBEAT_USER``
311
312 User to run beat as. Default is the current user.
313
314 * ``CELERYBEAT_GROUP``
315
316 Group to run beat as. Default is the current user.
317
318 * ``CELERY_CREATE_DIRS``
319
320 Always create directories (log directory and pid file directory).
321 Default is to only create directories when no custom logfile/pidfile set.
322
323 * ``CELERY_CREATE_RUNDIR``
324
325 Always create pidfile directory. By default only enabled when no custom
326 pidfile location set.
327
328 * ``CELERY_CREATE_LOGDIR``
329
330 Always create logfile directory. By default only enable when no custom
331 logfile location set.
332
333 .. _generic-initd-troubleshooting:
334
335 Troubleshooting
336 ----------------------------------------------------------------------
337
338 If you can't get the init-scripts to work, you should try running
339 them in *verbose mode*:
340
341 .. code-block:: console
342
343 # sh -x /etc/init.d/celeryd start
344
345 This can reveal hints as to why the service won't start.
346
347 If the worker starts with *"OK"* but exits almost immediately afterwards
348 and there's no evidence in the log file, then there's probably an error
349 but as the daemons standard outputs are already closed you'll
350 not be able to see them anywhere. For this situation you can use
351 the :envvar:`C_FAKEFORK` environment variable to skip the
352 daemonization step:
353
354 .. code-block:: console
355
356 # C_FAKEFORK=1 sh -x /etc/init.d/celeryd start
357
358
359 and now you should be able to see the errors.
360
361 Commonly such errors are caused by insufficient permissions
362 to read from, or write to a file, and also by syntax errors
363 in configuration modules, user modules, third-party libraries,
364 or even from Celery itself (if you've found a bug you
365 should :ref:`report it <reporting-bugs>`).
366
367
368 .. _daemon-systemd-generic:
369
370 Usage ``systemd``
371 ======================================================================
372
373 * `extra/systemd/`_
374
375 .. _`extra/systemd/`:
376 https://github.com/celery/celery/tree/main/extra/systemd/
377
378 .. _generic-systemd-celery:
379
380 :Usage: `systemctl {start|stop|restart|status} celery.service`
381 :Configuration file: /etc/conf.d/celery
382
383 Service file: celery.service
384 ----------------------------------------------------------------------
385
386 This is an example systemd file:
387
388 :file:`/etc/systemd/system/celery.service`:
389
390 .. code-block:: bash
391
392 [Unit]
393 Description=Celery Service
394 After=network.target
395
396 [Service]
397 Type=forking
398 User=celery
399 Group=celery
400 EnvironmentFile=/etc/conf.d/celery
401 WorkingDirectory=/opt/celery
402 ExecStart=/bin/sh -c '${CELERY_BIN} -A $CELERY_APP multi start $CELERYD_NODES \
403 --pidfile=${CELERYD_PID_FILE} --logfile=${CELERYD_LOG_FILE} \
404 --loglevel="${CELERYD_LOG_LEVEL}" $CELERYD_OPTS'
405 ExecStop=/bin/sh -c '${CELERY_BIN} multi stopwait $CELERYD_NODES \
406 --pidfile=${CELERYD_PID_FILE} --logfile=${CELERYD_LOG_FILE} \
407 --loglevel="${CELERYD_LOG_LEVEL}"'
408 ExecReload=/bin/sh -c '${CELERY_BIN} -A $CELERY_APP multi restart $CELERYD_NODES \
409 --pidfile=${CELERYD_PID_FILE} --logfile=${CELERYD_LOG_FILE} \
410 --loglevel="${CELERYD_LOG_LEVEL}" $CELERYD_OPTS'
411 Restart=always
412
413 [Install]
414 WantedBy=multi-user.target
415
416 Once you've put that file in :file:`/etc/systemd/system`, you should run
417 :command:`systemctl daemon-reload` in order that Systemd acknowledges that file.
418 You should also run that command each time you modify it.
419 Use :command:`systemctl enable celery.service` if you want the celery service to
420 automatically start when (re)booting the system.
421
422 Optionally you can specify extra dependencies for the celery service: e.g. if you use
423 RabbitMQ as a broker, you could specify ``rabbitmq-server.service`` in both ``After=`` and ``Requires=``
424 in the ``[Unit]`` `systemd section <https://www.freedesktop.org/software/systemd/man/systemd.unit.html#%5BUnit%5D%20Section%20Options>`_.
425
426 To configure user, group, :command:`chdir` change settings:
427 ``User``, ``Group``, and ``WorkingDirectory`` defined in
428 :file:`/etc/systemd/system/celery.service`.
429
430 You can also use systemd-tmpfiles in order to create working directories (for logs and pid).
431
432 :file: `/etc/tmpfiles.d/celery.conf`
433
434 .. code-block:: bash
435
436 d /run/celery 0755 celery celery -
437 d /var/log/celery 0755 celery celery -
438
439
440 .. _generic-systemd-celery-example:
441
442 Example configuration
443 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
444
445 This is an example configuration for a Python project:
446
447 :file:`/etc/conf.d/celery`:
448
449 .. code-block:: bash
450
451 # Name of nodes to start
452 # here we have a single node
453 CELERYD_NODES="w1"
454 # or we could have three nodes:
455 #CELERYD_NODES="w1 w2 w3"
456
457 # Absolute or relative path to the 'celery' command:
458 CELERY_BIN="/usr/local/bin/celery"
459 #CELERY_BIN="/virtualenvs/def/bin/celery"
460
461 # App instance to use
462 # comment out this line if you don't use an app
463 CELERY_APP="proj"
464 # or fully qualified:
465 #CELERY_APP="proj.tasks:app"
466
467 # How to call manage.py
468 CELERYD_MULTI="multi"
469
470 # Extra command-line arguments to the worker
471 CELERYD_OPTS="--time-limit=300 --concurrency=8"
472
473 # - %n will be replaced with the first part of the nodename.
474 # - %I will be replaced with the current child process index
475 # and is important when using the prefork pool to avoid race conditions.
476 CELERYD_PID_FILE="/var/run/celery/%n.pid"
477 CELERYD_LOG_FILE="/var/log/celery/%n%I.log"
478 CELERYD_LOG_LEVEL="INFO"
479
480 # you may wish to add these options for Celery Beat
481 CELERYBEAT_PID_FILE="/var/run/celery/beat.pid"
482 CELERYBEAT_LOG_FILE="/var/log/celery/beat.log"
483
484 Service file: celerybeat.service
485 ----------------------------------------------------------------------
486
487 This is an example systemd file for Celery Beat:
488
489 :file:`/etc/systemd/system/celerybeat.service`:
490
491 .. code-block:: bash
492
493 [Unit]
494 Description=Celery Beat Service
495 After=network.target
496
497 [Service]
498 Type=simple
499 User=celery
500 Group=celery
501 EnvironmentFile=/etc/conf.d/celery
502 WorkingDirectory=/opt/celery
503 ExecStart=/bin/sh -c '${CELERY_BIN} -A ${CELERY_APP} beat \
504 --pidfile=${CELERYBEAT_PID_FILE} \
505 --logfile=${CELERYBEAT_LOG_FILE} --loglevel=${CELERYD_LOG_LEVEL}'
506 Restart=always
507
508 [Install]
509 WantedBy=multi-user.target
510
511 Once you've put that file in :file:`/etc/systemd/system`, you should run
512 :command:`systemctl daemon-reload` in order that Systemd acknowledges that file.
513 You should also run that command each time you modify it.
514 Use :command:`systemctl enable celerybeat.service` if you want the celery beat
515 service to automatically start when (re)booting the system.
516
517 Running the worker with superuser privileges (root)
518 ======================================================================
519
520 Running the worker with superuser privileges is a very dangerous practice.
521 There should always be a workaround to avoid running as root. Celery may
522 run arbitrary code in messages serialized with pickle - this is dangerous,
523 especially when run as root.
524
525 By default Celery won't run workers as root. The associated error
526 message may not be visible in the logs but may be seen if :envvar:`C_FAKEFORK`
527 is used.
528
529 To force Celery to run workers as root use :envvar:`C_FORCE_ROOT`.
530
531 When running as root without :envvar:`C_FORCE_ROOT` the worker will
532 appear to start with *"OK"* but exit immediately after with no apparent
533 errors. This problem may appear when running the project in a new development
534 or production environment (inadvertently) as root.
535
536 .. _daemon-supervisord:
537
538 :pypi:`supervisor`
539 ======================================================================
540
541 * `extra/supervisord/`_
542
543 .. _`extra/supervisord/`:
544 https://github.com/celery/celery/tree/main/extra/supervisord/
545
546 .. _daemon-launchd:
547
548 ``launchd`` (macOS)
549 ======================================================================
550
551 * `extra/macOS`_
552
553 .. _`extra/macOS`:
554 https://github.com/celery/celery/tree/main/extra/macOS/