Detach the current process in the background (daemonize). Arguments: logfile (str): Optional log file. The ability to write to this file will be verified before the process is detached. pidfile (str): Optional pid file. The pidfile won't be cr
(logfile=None, pidfile=None, uid=None, gid=None, umask=0,
workdir=None, fake=False, **opts)
| 438 | |
| 439 | |
| 440 | def detached(logfile=None, pidfile=None, uid=None, gid=None, umask=0, |
| 441 | workdir=None, fake=False, **opts): |
| 442 | """Detach the current process in the background (daemonize). |
| 443 | |
| 444 | Arguments: |
| 445 | logfile (str): Optional log file. |
| 446 | The ability to write to this file |
| 447 | will be verified before the process is detached. |
| 448 | pidfile (str): Optional pid file. |
| 449 | The pidfile won't be created, |
| 450 | as this is the responsibility of the child. But the process will |
| 451 | exit if the pid lock exists and the pid written is still running. |
| 452 | uid (int, str): Optional user id or user name to change |
| 453 | effective privileges to. |
| 454 | gid (int, str): Optional group id or group name to change |
| 455 | effective privileges to. |
| 456 | umask (str, int): Optional umask that'll be effective in |
| 457 | the child process. |
| 458 | workdir (str): Optional new working directory. |
| 459 | fake (bool): Don't actually detach, intended for debugging purposes. |
| 460 | **opts (Any): Ignored. |
| 461 | |
| 462 | Example: |
| 463 | >>> from celery.platforms import detached, create_pidlock |
| 464 | >>> with detached( |
| 465 | ... logfile='/var/log/app.log', |
| 466 | ... pidfile='/var/run/app.pid', |
| 467 | ... uid='nobody'): |
| 468 | ... # Now in detached child process with effective user set to nobody, |
| 469 | ... # and we know that our logfile can be written to, and that |
| 470 | ... # the pidfile isn't locked. |
| 471 | ... pidlock = create_pidlock('/var/run/app.pid') |
| 472 | ... |
| 473 | ... # Run the program |
| 474 | ... program.run(logfile='/var/log/app.log') |
| 475 | """ |
| 476 | if not resource: |
| 477 | raise RuntimeError('This platform does not support detach.') |
| 478 | workdir = os.getcwd() if workdir is None else workdir |
| 479 | |
| 480 | signals.reset('SIGCLD') # Make sure SIGCLD is using the default handler. |
| 481 | maybe_drop_privileges(uid=uid, gid=gid) |
| 482 | |
| 483 | def after_chdir_do(): |
| 484 | # Since without stderr any errors will be silently suppressed, |
| 485 | # we need to know that we have access to the logfile. |
| 486 | logfile and open(logfile, 'a').close() |
| 487 | # Doesn't actually create the pidfile, but makes sure it's not stale. |
| 488 | if pidfile: |
| 489 | _create_pidlock(pidfile).release() |
| 490 | |
| 491 | return DaemonContext( |
| 492 | umask=umask, workdir=workdir, fake=fake, after_chdir=after_chdir_do, |
| 493 | ) |
| 494 | |
| 495 | |
| 496 | def parse_uid(uid): |