09 Process Management

Learning objectives

  • Inspect programs running in the system.
  • Stop a stuck or unneeded process.
  • Understand foreground jobs, background jobs, and long-running work.

Command overview

CommandPurposeMeaningBasic usageExample
psView a process snapshotProcess status; shows current process stateps [options]ps aux
topView processes liveDynamically shows CPU, memory, and processestoptop
htopMore friendly live process toolInteractive enhancement of top; may need installationhtophtop
pgrepFind and display matching processesVerify PID, name, and command line firstpgrep [options] namepgrep -a -x sleep
killSend a signal to a PIDSends SIGTERM by default and does not guarantee immediate exitkill PIDkill -TERM "$pid"
pkillSend a signal by matchCan match several processes; not used directly in beginner practicepkill [options] namepkill -TERM -x sleep
jobsView current Shell background jobsShows jobs started by this terminaljobsjobs
fgBring a background job to foregroundForeground; brings a job backfg %job_numberfg %1
bgContinue a suspended job in backgroundBackground; resumes a job in backgroundbg %job_numberbg %1
nohupRun while ignoring SIGHUP& is what backgrounds a Shell job; nohup does not provide restart, health checks, or service lifecyclenohup command >log 2>&1 &nohup python3 reviewed_app.py > agent-lab.log 2>&1 &

Common scenarios

Inspect Python-process clues for the current user:

pgrep -a -u "$(id -u)" -f 'python3'

Observe system processes live:

top

For a real process, do not copy example PIDs or use pkill with a broad name. First verify PID, owner, start time, parent process, and complete command, then consider a signal.

Run a command in the background:

python3 reviewed_app.py &
jobs
fg %1

This is interactive-syntax reference. Run it only after confirming the script path, arguments, and side effects. The runnable main exercise below uses sleep, created by the current Shell and saved by PID.

Run for a long time while preserving a log:

nohup python3 reviewed_app.py > agent-lab.log 2>&1 &

nohup does not govern a service; this course does not run this template.

Notes for beginners

  • A PID is a process identifier; kill needs a PID.
  • Default kill sends SIGTERM, which a program can handle or ignore. Wait and verify after sending it.
  • SIGKILL (often kill -9) cannot be caught and gives a process no cleanup opportunity. Use it only as a last resort after confirming the object, confirming TERM was ineffective, and receiving authorization.
  • procps-ng pgrep/pkill match process names by default, whose name field can be truncated. -f matches the full command line. pkill can stop multiple processes, so the main exercise does not use it.
  • jobs manages only background jobs started by the current Shell, not all system processes.
  • nohup primarily handles SIGHUP and redirection; it does not provide restart, health checks, or service lifecycle. tmux is not a service manager either.

Terminate only an experimental process you started

sleep 300 &
pid=$!
ps -p "$pid" -o pid=,ppid=,user=,lstart=,comm=,args=
if [ "$(ps -o uid= -p "$pid" | tr -d ' ')" != "$(id -u)" ]; then
  printf 'owner mismatch\n' >&2
  exit 1
fi
kill -TERM "$pid"
if wait "$pid"; then
  wait_rc=0
else
  wait_rc=$?
fi
printf 'wait_rc=%s\n' "$wait_rc"
if kill -0 "$pid" 2>/dev/null; then
  printf 'process still exists\n' >&2
  exit 1
fi

wait commonly returns nonzero for a child ended by SIGTERM. That reflects the signal outcome and should not be silently cut short by set -e. kill -0 only checks clues about signal permission/process existence, and PIDs can be reused; hand real services to a service manager.

References