Linux & the Command Line

Processes, Jobs and System Resources

25 min

A process is a running program. Each has a PID (process ID), an owner and resource usage.

Seeing processes

ps aux                    # every process
ps aux | grep python      # find one
top                       # live view (q to quit)
htop                      # friendlier live view, if installed
pgrep -a gunicorn         # PIDs and command lines by name

In top, press M to sort by memory and P by CPU.

Stopping processes

kill 4321                 # ask politely (SIGTERM): lets it clean up
kill -9 4321              # force (SIGKILL): last resort
pkill -f "celery worker"  # by name pattern

Always try plain kill first. -9 gives the program no chance to save data or close connections.

Foreground and background jobs

python long_task.py &     # start in the background
jobs                      # list background jobs
fg %1                     # bring job 1 to the foreground
Ctrl+Z, then bg           # pause a running command, resume it in background
nohup ./backup.sh &       # keep running after you log out

On servers, prefer proper services (systemd, covered in Linux System Administration) over nohup for anything long-lived.

Resources

free -h                   # memory
df -h                     # disk space per filesystem
du -sh *                  # size of each item here
du -sh ~/.cache           # how big is one folder
uptime                    # load average over 1, 5, 15 minutes
nproc                     # number of CPU cores

A load average higher than the number of cores means work is queueing. A full disk (df -h at 100%) is behind a surprising number of "the database crashed" incidents.

Worked example: the server is slow

uptime                    # load 7.8 on a 2-core box: overloaded
top                       # one python process at 190% CPU
ps -o pid,etime,cmd -p 9123   # running for 3 hours: a stuck job
kill 9123

Practice

Complete the Stop a stuck process mission: list processes with ps, then kill 777.

Check yourself

  • What is the difference between kill and kill -9?
  • Which command shows free disk space?
  • What does a load average of 4 mean on a 4-core machine?

Your progress is saved in this browser only.

Submit for review (10 points)