Process Management and Job Control

See what is running on your system, stop misbehaving programs, and run tasks in the background while you keep working.

~7 min Beginner

What is a process?

Every time you run a program, Linux creates a process. A process is one running instance of a program. Your web browser is a process, the terminal window is a process, and the ls command is a process that lasts a fraction of a second.

Each process gets a unique number called the PID (process ID). Linux and you use the PID to refer to one specific process.

Seeing what is running

ps: a snapshot

ps
ps aux
ps aux | grep firefox
  • ps lists the processes started from your current terminal.
  • ps aux lists processes from all users: a for all users, u for a detailed format and x to include processes that have no terminal.
  • Piping into grep filters the list to one program.

Useful columns in ps aux are USER, PID, %CPU, %MEM and COMMAND.

top and htop: a live view

top
htop

top shows a list that updates every few seconds, with the busiest processes at the top. Press q to quit. htop is a friendlier version that you may need to install. When your computer feels slow, this is where to look first.

pgrep: find a PID by name

pgrep firefox

pgrep prints the PID of every process whose name matches.

Stopping processes

kill 1234
kill -9 1234
killall firefox
  • kill PID sends the process a polite request to stop, called SIGTERM. The program gets a chance to save its work and clean up.
  • kill -9 PID sends SIGKILL, which cannot be ignored. The system ends the process immediately and the program gets no time to save anything.
  • killall name sends the signal to every process with that name.

Always try the polite kill first. Use kill -9 only when a program does not respond. A running command in your terminal can also be stopped with Ctrl + C, which is the gentlest option of all.

The order from gentlest to most forceful is: Ctrl + C, then kill PID, then kill -9 PID.

Background jobs

A command normally occupies the terminal until it finishes, which is called running in the foreground. Add & at the end to run it in the background so you get the prompt back at once.

sleep 60 &
jobs
fg %1
  • sleep 60 & waits 60 seconds in the background.
  • jobs lists the background jobs of this terminal, each with a job number.
  • fg %1 brings job number 1 back to the foreground.

Pausing and resuming

If you forgot the &, you can still move a running command to the background:

  1. Press Ctrl + Z to pause it.
  2. Type bg to resume it in the background.

You can also bring any job back with fg.

  • Ctrl + C stops the foreground program.
  • Ctrl + Z pauses it.
  • Ctrl + D ends input, and on an empty prompt closes the shell.

Keep running after you log out

When you close a terminal, the shell sends its jobs a hangup signal and they stop. nohup ("no hangup") tells a command to ignore that signal.

nohup ./task.sh > out.log 2>&1 &

The command is run in the background (&), keeps running after you close the terminal and writes its output to out.log. Without the redirection, nohup saves output to a file called nohup.out.

Sign up to track your progress Mark lessons as read and pick up where you left off Sign up free to unlock this practice Your first practice lesson is free to try. Create an account to unlock the rest and save your progress