Find a course

Module 4: Permissions, Users and ProcessesLesson 11 of 1311 min

Processes, Jobs and Services

See what is running with ps and top, stop programs with kill and signals, run jobs in the background, and manage services with systemctl.

Step 1 of 13Learn

The server is slow

At 2 p.m. users complain that the internal web app is crawling. Andre connects to the server and, within a minute, finds a forgotten data export that has been eating 100% of a CPU core since the morning. He stops it, and the app recovers. Everything he did used four commands from this lesson.

Every running program is a process. Each process has a process ID (PID), an owner, a parent process that started it, and a share of CPU and memory.

  • ps shows processes started from your current terminal.
  • ps aux shows every process on the system: the owner (USER), PID, CPU and memory percentages, and the full command line. Pipe it to grep to find something: ps aux | grep python.
  • top shows a live, updating view sorted by CPU use. Press M to sort by memory, k to kill a process by PID, and q to quit. htop is a friendlier, colorful version you may need to install.
  • pgrep -l nginx prints PIDs of processes by name.

The top of top shows the load average: three numbers for the last 1, 5 and 15 minutes. Roughly, it is the average number of processes running or waiting to run. On a machine with 4 CPU cores, a load of 4 means fully busy; a sustained load of 12 means work is piling up. nproc tells you how many cores you have.

Step 2 of 13Practice · Diagram

Load average on a 4-core server

Compare the load to the number of cores (nproc). Tap a marker.

Which marker means the server is fully busy but not yet queueing work?

Tap the shape that answers the question, then press Check.

Load average on a 4-core serverScale: Load 1, Load 4, Load 12.Spare capacityBusy, some waitingWork piling up02468101214161Load 14Load 412Load 12
A rough rule of thumb; disk waits also add to load on Linux.
Load 1

About one core busy; three idle.

Load 4

Every core busy, nothing waiting.

Load 12

Three times the cores: processes queue and users notice.

Step 3 of 13Practice · Work it out

A server has 8 CPU cores. top shows load averages of 15.2, 14.8, 13.9. Roughly how many times its core count is the 1-minute load? Round to one decimal place.

Step 4 of 13Learn

Stopping processes with signals

You stop a process by sending it a signal, a small numbered message from the kernel. The command is confusingly named kill, though most signals are polite requests.

SignalNumberMeaning
SIGTERM15"Please shut down." The default for kill. The program can save work and clean up.
SIGKILL9"Stop now." The kernel ends the process immediately; it cannot be caught or ignored.
SIGINT2Interrupt, what Ctrl+C sends to the foreground program.
SIGHUP1Hang up; many services reload their configuration when they receive it.
$ kill 4821         # sends SIGTERM to PID 4821
$ kill -9 4821      # sends SIGKILL
$ pkill -f export.py   # by name or command line

The right order is TERM first, KILL only if needed. SIGTERM gives a database the chance to finish writing and close its files. SIGKILL can leave half-written files, stale lock files or corrupted data behind. Wait a few seconds after kill, check with ps, and escalate to kill -9 only if the process is truly stuck.

You can signal your own processes freely. Signaling processes owned by other users requires sudo.

Step 5 of 13Practice · Put in order

A runaway process (PID 7310) is hogging the CPU. Put a careful response in order.

Drag the items, or use the arrow buttons, to put them in order.

  1. Check what it is with ps -p 7310 -f
  2. Wait a few seconds and check whether it has exited
  3. Only if it is still running, send SIGKILL with kill -9 7310
  4. Send it SIGTERM with kill 7310
  5. Run top to identify the process using the most CPU

Step 6 of 13Compare

SIGTERM vs SIGKILL

AspectSIGTERM (15)SIGKILL (9)
Sent bykill PID (the default)kill -9 PID
Can the program react?Yes: save, clean up, exitNo: the kernel ends it at once
RiskLowHalf-written files, stale locks
UseFirst, alwaysOnly if TERM did not work

Step 7 of 13Learn

Foreground, background and jobs

When you run a command, it normally runs in the foreground: your prompt doesn't come back until it finishes. For long tasks, the shell's job control lets you keep working.

  • Add & at the end to start a command in the background: ./export.sh &. The shell prints a job number and PID and gives you the prompt back.
  • Press Ctrl+Z to pause (suspend) the foreground command.
  • jobs lists the jobs belonging to this shell.
  • bg resumes a paused job in the background; fg brings a job back to the foreground (fg %2 for job number 2).

One catch: background jobs normally die when you close the terminal or your SSH connection drops, because the shell sends them SIGHUP. For long-running tasks on a remote server, start them with nohup command &, which ignores the hang-up signal and writes output to nohup.out, or run them inside a terminal multiplexer like tmux or screen, which keeps a session alive on the server so you can reconnect later.

A common workflow: you start a big copy, realize it will take an hour, press Ctrl+Z, type bg, and carry on working while it finishes.

Step 8 of 13Practice · Match the pairs

Match each action to the command or key that does it.

Tap an answer, then tap the place where it belongs (or drag it there).

  1. Start a script in the background
  2. Pause the current foreground program
  3. Resume a paused job in the background
  4. Bring a background job to the foreground
  5. List this shell's jobs

Step 9 of 13Diagram

Moving a job around

Moving a job aroundCycle diagram: Running in foreground → Ctrl+Z: paused → bg: running in background → fg: back to the front.Job controlRunning inforegroundCtrl+Z: pausedbg: running inbackgroundfg: back to thefront

Tap a shape with to see more.

Running in foreground

You wait for the prompt.

Ctrl+Z: paused

The job is suspended, the prompt comes back.

bg: running in background

It keeps working while you type other commands.

fg: back to the front

fg %2 picks job number 2 from jobs.

Step 10 of 13Learn

Services and systemd

Web servers, databases and SSH aren't started by hand. They are services (also called daemons): background processes that start at boot and run without a terminal. On almost every modern distribution they are managed by systemd, controlled with systemctl:

$ systemctl status nginx          # is it running? recent log lines
$ sudo systemctl restart nginx    # stop and start
$ sudo systemctl reload nginx     # re-read config without dropping connections
$ sudo systemctl enable nginx     # start automatically at boot
$ sudo systemctl disable nginx    # don't start at boot

start and stop affect the service right now; enable and disable affect what happens at the next boot. A service can be running but disabled (it will be gone after a reboot) or enabled but stopped. sudo systemctl enable --now nginx does both at once.

When a service fails, read its logs with journalctl -u nginx. Add -f to follow new entries live, like tail -f, or --since "10 min ago" to narrow the window. A good troubleshooting loop is: edit the config, check it if the program offers a test (nginx -t), restart, then look at systemctl status and the journal.

Step 11 of 13Practice · Diagram

Running now vs starting at boot

start/stop act on the present; enable/disable act on the next boot. Tap a box.

Where does sudo systemctl enable --now nginx put the service?

Tap the shape that answers the question, then press Check.

Running now vs starting at bootTwo-by-two matrix: Enabled but stopped, Enabled and running, Disabled and stopped, Running but disabled.Enabled but stoppedEnabled and runningDisabled and stoppedRunning but disabledRight nowStoppedRunningAt next bootStays offStarts
Enabled but stopped

Down now, comes up after a reboot.

Enabled and running

The normal state for a production service.

Disabled and stopped

Off now and after reboot.

Running but disabled

Works today, gone after the next reboot.

Step 12 of 13Note

Step 13 of 13Practice · What would you do?

What would you do?

After a reboot, your company's web server isn't serving pages. systemctl status nginx says inactive (dead) and disabled. Yesterday you started it with sudo systemctl start nginx and it worked. What happened and what is the fix?