Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Computer Basics / The Linux Command Line

    Processes and Jobs

    The hotel manager's register of guests from 02operatingsystems.md — now you get to read it, and ask guests to leave.

    Chapter 7 of 123 min read

    The Plain Picture#

    The hotel manager's register of guests from Operating Systems — now you get to read it, and ask guests to leave.

    The Real Names and Commands#

    Terminal
    ps aux                           # every process: user, PID, %CPU, %MEM, command
    ps aux | grep uvicorn            # find the StudyBuddy server process
    pgrep -fl uvicorn                # the same, more neatly: PIDs + command lines
    top                              # live view sorted by CPU; q to quit (htop is a friendlier version)
    kill 4242                        # send SIGTERM to PID 4242 — "please stop" (02_operating_systems.md Ch 12)
    kill -9 4242                     # SIGKILL — "die now", no clean-up. Last resort only
    pkill -f uvicorn                 # signal every process whose command line matches

    Jobs — running things in the background of your shell:

    Terminal
    python3 long_task.py &           # & = start it in the background; you get the prompt back
    jobs                             # list this shell's background jobs
    fg %1                            # bring job 1 back to the foreground
    # Ctrl+Z pauses the foreground job; then `bg` resumes it in the background
    nohup python3 long_task.py > task.log 2>&1 &   # keep running even after you log out

    Reading top's header:

    Output
      load average: 3.52, 2.10, 1.05

    The load average is roughly the number of processes running or waiting to run, averaged over the last 1, 5 and 15 minutes. Compare it with the number of cores (nproc on Linux, sysctl -n hw.ncpu on Mac): load 3.5 on 8 cores is fine; load 30 on 8 cores means everything is queueing. Rising numbers (1.05 → 2.10 → 3.52) mean the load is growing.

    Gotchas

    • kill -9 first is a bad habit. The process can't flush buffers, finish requests or remove lock files. Always try plain kill (SIGTERM) and wait a few seconds.

    • Background jobs with & die when you close the SSH session unless you use nohup — or, much better on servers, run things as proper services (Ch 11), or inside tmux (a terminal that keeps running on the server after you disconnect).

    Check yourself

    4 questions
    1

    How do you find the PID of a running uvicorn server?

    Show answer

    pgrep -fl uvicorn (or ps aux | grep uvicorn).

    2

    kill vs kill -9?

    Show answer

    kill sends SIGTERM (graceful, can be handled); kill -9 sends SIGKILL (immediate, can't be caught).

    3

    A server with 4 cores has load average 12.0, 11.5, 11.8. What does it mean?

    Show answer

    About 3× more runnable work than cores, steadily for 15 minutes — it's overloaded.

    4

    How do you keep a long command running after you log out?

    Show answer

    nohup cmd > log 2>&1 &, or tmux, or better, run it as a systemd service.