{
    "slug": "php_pcntl_signals",
    "term": "PCNTL Signals in CLI PHP",
    "category": "php",
    "difficulty": "advanced",
    "short": "The PCNTL extension lets CLI PHP scripts handle OS signals (SIGTERM, SIGINT, SIGUSR1) — enabling graceful shutdown and hot reload in long-running daemons.",
    "long": "pcntl_signal(SIGTERM, $handler) registers a callback for OS signals. Signals are checked at tick points (declare(ticks=1)) or via pcntl_signal_dispatch() in loops. Key signals: SIGTERM (graceful shutdown request), SIGINT (Ctrl+C), SIGUSR1/USR2 (custom), SIGHUP (reload config). Pattern for queue workers: set a flag on SIGTERM, check flag in loop, finish current task then exit cleanly. PHP-FPM manages its own signals separately. The pcntl extension is CLI-only — not available in FPM/web context. PHP 8.1: async signals via pcntl_async_signals(true) removes need for ticks.",
    "aliases": [],
    "tags": [
        "php",
        "cli",
        "signals",
        "pcntl",
        "daemon"
    ],
    "misconception": "PHP CLI scripts terminate immediately on SIGTERM — by default yes, but with pcntl_signal() you can intercept and handle gracefully.",
    "why_it_matters": "Graceful shutdown on SIGTERM prevents data corruption in queue workers and daemons — the job in progress completes before the process exits.",
    "common_mistakes": [
        "Not calling pcntl_signal_dispatch() in tight loops — signals accumulate but handler never fires.",
        "Using ticks=1 on every statement — performance overhead. Use pcntl_async_signals(true) instead.",
        "Handling SIGKILL — impossible to catch, always terminates immediately."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "php_sapi_types",
        "php_cli",
        "php_queue_workers",
        "async_processing"
    ],
    "prerequisites": [
        "php_sapi_types",
        "php_cli"
    ],
    "refs": [
        "https://www.php.net/manual/en/book.pcntl.php"
    ],
    "bad_code": "// Worker that can't shut down gracefully:\nwhile (true) {\n    $job = $queue->pop();\n    processJob($job); // Killed mid-job on deploy\n}",
    "good_code": "pcntl_async_signals(true); // PHP 8.1+ — no ticks needed\n$running = true;\npcntl_signal(SIGTERM, function() use (&$running) {\n    $running = false;\n});\npcntl_signal(SIGINT, function() use (&$running) {\n    $running = false;\n});\n\nwhile ($running) {\n    $job = $queue->pop();\n    if ($job) processJob($job); // Completes current job before exit\n    else usleep(100000);\n}",
    "quick_fix": "Use pcntl_async_signals(true) (PHP 8.1+) instead of ticks. Set a $running flag on SIGTERM/SIGINT. Check flag in worker loop to exit cleanly after current task.",
    "severity": "medium",
    "effort": "medium",
    "created": "2026-03-23",
    "updated": "2026-03-23",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/php_pcntl_signals",
        "html_url": "https://codeclaritylab.com/glossary/php_pcntl_signals",
        "json_url": "https://codeclaritylab.com/glossary/php_pcntl_signals.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[PCNTL Signals in CLI PHP](https://codeclaritylab.com/glossary/php_pcntl_signals) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/php_pcntl_signals"
            }
        }
    }
}