Observe and stop processes

Lesson 19 of 30 · Working with Linux

A process ID (PID) identifies a process while it exists, but the system may reuse that number after it exits. On Linux, ps aux displays a snapshot including user, PID, CPU, memory, and command. top presents a changing view; on common Linux versions q exits and uppercase M sorts by memory. Display and options vary. A high number by itself does not prove a process is faulty, and one program may start several processes.

Ctrl+C in a terminal normally sends SIGINT to the foreground process group, requesting interruption. A process can catch or ignore that signal. kill sends a signal; despite its name it does not always immediately terminate the target. SIGTERM requests termination and allows a program to handle cleanup; SIGKILL cannot be caught and may lose work. If a process is stuck, first identify its current PID, owner, and command; do not use an old PID or target an unknown or system-critical process.

This contained example signals only the sleep process just started by the same shell:

sleep 60 &
pid=$!
ps -p "$pid" -o pid=,comm=
kill -TERM "$pid"
wait "$pid" 2>/dev/null || :

& starts the process in the background, and $! captures its PID immediately. The ps line displays the current PID and command if it is still running. kill -TERM sends SIGTERM to that positive PID; wait collects the child’s status. Shells may print a termination message. If the signal is not sent, this particular process normally ends after 60 seconds. In real troubleshooting check the process again immediately before signalling; a PID can be reused, and permissions or process state can prevent the request.