{
    "slug": "linux_processes",
    "term": "Linux Processes",
    "category": "linux",
    "difficulty": "intermediate",
    "short": "Every running program is a process with a PID, memory space, and file descriptors — ps, top, kill, and signals are the essential tools for managing them.",
    "long": "Each process has a PID (process ID), parent PID (PPID), memory map, open file descriptors, and a signal mask. Key tools: ps aux (snapshot of all processes), top/htop (live view), kill -SIGNAL PID (send signal), strace (trace system calls), lsof (open files per process). Signals: SIGTERM (graceful shutdown request), SIGKILL (immediate termination, cannot be caught), SIGHUP (reload config), SIGUSR1/SIGUSR2 (custom). PHP-FPM responds to SIGTERM for graceful shutdown and SIGUSR2 for reload.",
    "aliases": [
        "PID",
        "process management",
        "signals",
        "kill command"
    ],
    "tags": [
        "linux",
        "devops",
        "php",
        "processes"
    ],
    "misconception": "kill kills a process immediately — kill sends SIGTERM by default, which asks the process to shut down gracefully; only kill -9 (SIGKILL) forces immediate termination.",
    "why_it_matters": "Understanding signals is essential for zero-downtime PHP-FPM reloads, graceful queue worker shutdown, and diagnosing hung processes — kill -9 should always be the last resort.",
    "common_mistakes": [
        "Using kill -9 as the first option — SIGKILL prevents cleanup, open file handles stay open, transactions may be left incomplete.",
        "Not handling SIGTERM in queue workers — workers killed mid-job corrupt the queue state.",
        "ps aux grep includes the grep process itself in results — use ps aux | grep [p]rocess to exclude it.",
        "Not knowing that zombie processes are already dead — they are waiting for the parent to read their exit code; kill the parent or fix the parent to wait()."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "php_fpm",
        "processes_vs_threads",
        "cron_jobs",
        "bash_scripting"
    ],
    "prerequisites": [
        "linux_file_permissions",
        "linux_user_management",
        "php_fpm"
    ],
    "refs": [
        "https://man7.org/linux/man-pages/man7/signal.7.html"
    ],
    "bad_code": "# Brutal kill — no cleanup, potential data loss:\nkill -9 $(pgrep php-fpm)   # Immediately kills all FPM workers\n# Active requests terminated mid-execution\n# DB transactions left open\n# Temp files not cleaned up",
    "good_code": "# Graceful PHP-FPM reload (zero-downtime):\nkill -USR2 $(cat /var/run/php-fpm.pid)  # Reload config, finish active requests\n\n# Graceful shutdown (waits for active requests):\nkill -TERM $(cat /var/run/php-fpm.pid)  # SIGTERM — graceful\n\n# Monitor:\nwatch -n1 'ps aux | grep php-fpm | grep -v grep'\n\n# Queue worker: handle SIGTERM gracefully in PHP:\npcntl_async_signals(true);\npcntl_signal(SIGTERM, function() use (&$running) { $running = false; });",
    "quick_fix": "Use ps aux, top, or htop to identify runaway PHP processes; set memory_limit and max_execution_time in php.ini to prevent resource exhaustion",
    "severity": "medium",
    "effort": "low",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/linux_processes",
        "html_url": "https://codeclaritylab.com/glossary/linux_processes",
        "json_url": "https://codeclaritylab.com/glossary/linux_processes.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": "[Linux Processes](https://codeclaritylab.com/glossary/linux_processes) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/linux_processes"
            }
        }
    }
}