Processes and Jobs

What's running, how to stop it, and how to run things in the background.

Updated

Processes

  1. What's Running: ps, top and htop (Apprentice): Every running program is a process with a number, its PID: ps lists the ones in your terminal, ps aux lists every one on the computer, ps aux --sort=-%cpu puts the busiest first, and top and htop show the same list live.
  2. Stopping Things: kill and Signals (Journeyman): kill sends a process a signal: plain kill sends TERM, a polite request to finish up and stop; kill -KILL ends a process that ignores it; pgrep -af and pkill -f find and stop processes by name; and Ctrl+C sends INT to the program in front of you.

Jobs

  1. Background Jobs: &, jobs, fg and bg (Journeyman): An & at the end of a command runs it in the background and gives you the prompt back; jobs lists what's running, Ctrl+Z pauses the job in front, fg and bg bring a job back or let it carry on behind you, and nohup keeps a job running after the terminal closes.

Exercise: Stop the Runaway

  1. Exercise: Stop the Runaway (Journeyman): Find and stop three misbehaving programs with ps, pgrep, kill and pkill, then write two small tools, one that stops any process the polite way before it uses force and one that starts a long export so it survives the terminal closing, and let a check script tell you what's done.