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.
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
pslists the processes started from your current terminal.ps auxlists processes from all users:afor all users,ufor a detailed format andxto include processes that have no terminal.- Piping into
grepfilters 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 PIDsends the process a polite request to stop, called SIGTERM. The program gets a chance to save its work and clean up.kill -9 PIDsends SIGKILL, which cannot be ignored. The system ends the process immediately and the program gets no time to save anything.killall namesends 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.jobslists the background jobs of this terminal, each with a job number.fg %1brings job number 1 back to the foreground.
Pausing and resuming
If you forgot the &, you can still move a running command to the background:
- Press
Ctrl + Zto pause it. - Type
bgto resume it in the background.
You can also bring any job back with fg.
Ctrl + Cstops the foreground program.Ctrl + Zpauses it.Ctrl + Dends 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.