Skip to content
Visual explainer 3h to learn 5 steps

Processes, signals & priorities

Find, prioritise and stop processes with ps, top, kill and nice.

By Invicly Tech, , in Linux Administration (RHCSA-style). About 11 minutes to read.

Topic 13 of 42

In short

Every running program is a process with a PID, an owner and a parent. ps and top show what is running and how much CPU and memory it uses, kill sends signals to stop or reload processes, and nice and renice change scheduling priority. Shell job control moves commands between foreground and background.

Explained simply

A process is a program that is running right now. Each one has a number called the PID (process ID), a user it runs as, and a parent process that started it. A busy server runs hundreds of them at once, most of them quietly waiting.

ps takes a snapshot of the processes, and top shows a live view that refreshes every few seconds. With them you can answer the first questions in any incident: what is running, who started it, and what is using all the CPU or memory?

You talk to processes with signals, which are small numbered messages. The polite one, SIGTERM, asks a program to finish its work and exit. The forceful one, SIGKILL, ends it instantly with no chance to clean up. kill, pkill and killall send signals.

You can also change how much CPU time a process gets with its nice value, and move commands you started between the foreground and the background of your shell. That lets a long backup run without slowing down the web server or blocking your terminal.

Press Play on a diagram to walk through it one step at a time.

MapA typical process tree (pstree)
systemd (PID 1)
  • sshd
    • sshd: ana [session]
    • bash
    • top
  • httpd
    • httpd worker
    • httpd worker
  • chronyd
  • systemd --user
    • podman container
FlowStopping a misbehaving process, politely first
  1. 1
    Identify it

    pgrep -a or ps -ef, check the parent

  2. 2
    Is it a service?

    Then use systemctl stop or restart

  3. 3
    Send SIGTERM

    kill PID

  4. 4
    Wait a few seconds

    kill -0 PID checks if it is gone

  5. 5
    Send SIGKILL

    kill -9 PID only as a last resort

In depth

Signals you will actually use

SIGTERM (15) is the default for kill and asks a process to stop; well-written programs close files and finish requests first. SIGKILL (9) is handled by the kernel, not the program, so it cannot be caught or ignored, and nothing is cleaned up: lock files, temporary files and half-written data stay behind. Use it only after SIGTERM has had a few seconds.

SIGHUP (1) originally meant the terminal hung up; many daemons treat it as reload your config. SIGINT (2) is what Ctrl-C sends. SIGSTOP pauses a process and SIGCONT resumes it. kill -l lists all signals, and kill -0 PID sends nothing but tells you whether the process exists and you may signal it.

A process stuck in state D (waiting on disk or NFS) will not die even from SIGKILL until the I/O returns.

  • pgrep -a nginx lists PIDs with full command lines
  • pkill -u alice sends SIGTERM to all of alice's processes
  • pkill -f matches the whole command line, so be careful with short patterns
  • pgrep -x sshd matches the exact process name only
CompareSIGTERM vs SIGKILL vs SIGHUP
SIGTERM (15)
  • Default for kill and systemctl stop
  • Program can clean up
  • Can be caught or ignored
SIGKILL (9)
  • Kernel ends the process at once
  • No clean-up at all
  • Cannot be caught or ignored
SIGHUP (1)
  • Terminal closed, or reload request
  • Many daemons re-read config
  • Kills jobs left in a closed shell

Reading top like an admin

The first lines of top summarise the whole machine. The load average shows how many tasks were running or waiting over 1, 5 and 15 minutes; compare it with the number of CPUs from nproc. The %Cpu(s) line splits time into us (user programs), sy (kernel), ni (niced programs), id (idle), wa (waiting for disk) and st (stolen by the hypervisor on a VM). High wa points to storage, high st to an overloaded host.

For memory, look at avail Mem, not free. Linux uses spare RAM as disk cache (buff/cache) and gives it back when programs need it, so a small free number is normal.

Useful keys: P sorts by CPU, M by memory, 1 shows each CPU, c shows full commands, u filters by user, k kills and r renices. A %CPU above 100 means the process uses more than one core.

LayersThe top header, line by line
  1. 1
    Uptime and load average

    Compare the three numbers with nproc

  2. 2
    Tasks

    Total, running, sleeping, stopped, zombie

  3. 3
    %Cpu(s)

    us, sy, ni, id, wa, st

  4. 4
    MiB Mem and Swap

    avail Mem is what programs can still get

  5. 5
    Process list

    PID, USER, PR, NI, RES, %CPU, %MEM, COMMAND

Nice, renice and fair sharing

The kernel scheduler shares CPU time between runnable processes. The nice value, from -20 to 19, changes each process's share. The default is 0. Each step is worth roughly 10 percent of CPU share relative to a neighbour, so a process at nice 19 gets only a small slice when the CPU is busy. On an idle CPU nice changes nothing; it only matters when processes compete.

nice -n 10 cmd starts a command with a lower priority and renice -n 10 -p PID changes a running one. Normal users may only make their processes nicer (raise the number); lowering it, even back to where it was, needs root. In top, PR is 20 plus the nice value.

CPU is not the only shared resource. ionice -c3 cmd gives a process the idle I/O class, so it only uses the disk when nobody else needs it. For services, set Nice= or CPUWeight= in the unit file.

  • nice -n 19 ionice -c3 tar czf /backup/home.tgz /home
  • renice -n 5 -u alice lowers all of alice's processes
  • ps -o pid,ni,cmd -p PID shows the current nice value

Job control and surviving logout

A command ending in & runs in the background and the shell prints its job number and PID. Ctrl-Z sends SIGTSTP and pauses the foreground command. bg resumes it in the background, fg brings it back to the foreground, and jobs -l lists them. %1 refers to job 1, so kill %1 works too.

Jobs belong to your shell. When the SSH connection drops, the shell gets SIGHUP and passes it on, which stops your jobs. nohup cmd & ignores SIGHUP and writes output to nohup.out, and disown removes a running job from the shell's list. For anything important, run it inside tmux or screen, which keep running on the server, or start it as a transient systemd unit with systemd-run, so it is supervised and logged like a service.

CycleA job moving between states
  1. 1
    Foreground

    Owns the terminal

  2. 2
    Ctrl-Z

    Paused (stopped) with SIGTSTP

  3. 3
    bg

    Runs on in the background

  4. 4
    fg

    Back to the foreground

  5. Back to step 1

Learn it in 5 short steps

About 35m each. Track them with a timer in your plan.

  1. 1
    List processes with ps and pgrep~35m
  2. 2
    Watch usage interactively in top~35m
  3. 3
    Send signals with kill and pkill~35m
  4. 4
    Adjust priority with nice and renice~35m
  5. 5
    Manage jobs with bg, fg and jobs~35m

Key points

  • ps aux lists all processes; ps -ef shows parent PIDs.
  • kill sends SIGTERM by default; use kill -9 only when SIGTERM fails.
  • Nice values run from -20 (highest priority) to 19 (lowest); only root can lower them.
  • In top, P sorts by CPU, M by memory and k kills a process.

Key terms

Tap a card to flip it. 0/5 known

Examples

bash
ps aux --sort=-%cpu | head -5
pgrep -a sshd
nice -n 10 tar czf /tmp/etc.tgz /etc &
sudo renice -n 5 -p $(pgrep -f backup.sh)
kill -HUP $(pgrep -o rsyslogd)    # ask it to reload
pkill -u baduser

Find heavy processes, change priorities and send signals.

Run a heavy job without hurting the website

You must compress 50 GB of logs during the day on a web server. Step 1: start the job with the lowest CPU priority and the idle I/O class, in the background, with its output in a file. Step 2: check that the nice value and I/O class were applied. Step 3: if it still gets in the way, pause it with SIGSTOP and resume it later with SIGCONT, without losing any work.

bash
nohup nice -n 19 ionice -c3 tar czf /backup/logs.tgz /var/log/archive > /tmp/tar.log 2>&1 &
PID=$!
ps -o pid,ni,stat,cmd -p "$PID"
ionice -p "$PID"            # idle
kill -STOP "$PID"            # pause during peak traffic
kill -CONT "$PID"            # carry on later

Recover a command you started in the foreground

You started a long rsync in your SSH session and now need the terminal, and you are worried the connection may drop. Step 1: press Ctrl-Z to pause it. Step 2: bg lets it continue in the background. Step 3: disown -h tells the shell not to send it SIGHUP when you log out. Step 4: confirm later from a new session that it is still running.

bash
rsync -a /srv/data/ backup:/srv/data/
# press Ctrl-Z
# [1]+  Stopped   rsync -a /srv/data/ backup:/srv/data/
bg
jobs -l
disown -h %1
# later, from a new SSH session:
pgrep -a rsync

Quick reference

  • ps -eo pid,ppid,user,ni,stat,%cpu,%mem,cmd --sort=-%mem | head shows top memory users
  • pgrep -a NAME lists matching PIDs with full commands; pkill -x NAME signals exact names
  • kill -0 PID checks if a process exists without affecting it
  • kill -HUP PID asks many daemons to reload their config
  • nice -n 10 cmd starts lower priority; renice -n 10 -p PID changes a running process
  • In top: P CPU, M memory, 1 per-CPU, k kill, r renice, c full command
  • avail Mem in top or free -h is the memory programs can still use
  • Ctrl-Z pauses, bg resumes in background, fg returns, disown detaches from the shell

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1A process ignores kill PID. What should you do before reaching for kill -9?

Question 2

2As a normal user you ran renice -n 10 on your job. Can you set it back to 0 yourself?

Question 3

3top shows 30 wa in the %Cpu(s) line. What does it suggest?

Question 4

4Which signal cannot be caught or ignored by a program?

Question 5

5A process at nice 19 runs on an otherwise idle 4-core server. How fast does it run?

0/5 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Start sleep 600 three times in the background, list them as jobs, bring the second one to the foreground, then stop all of them with a single command.

  2. Exercise 2medium

    Find the three processes using the most memory, show their owner, nice value and full command, and lower the priority of the biggest one to nice 10.

  3. Exercise 3hard

    Write a small function stopwait PID that sends SIGTERM, waits up to 10 seconds for the process to exit, and only then sends SIGKILL. It should print what it did.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

What is the difference between kill, pkill and killall?
kill takes PIDs. pkill takes a pattern and matches names or, with -f, full command lines. killall takes exact process names; on Linux it comes from the psmisc package.
Why is my free memory so low when the server is idle?
Linux uses spare RAM as a disk cache and frees it on demand. Look at the available column of free -h, not the free column.
What is a zombie process and should I kill it?
A zombie has already exited and only waits for its parent to read its exit status. You cannot kill it; fix or restart the parent so it collects or hands over its children.
How do I keep a command running after I log out?
Use tmux or screen, or start it with nohup or systemd-run. A plain background job usually dies when the shell gets SIGHUP.

Go deeper

ShareXLinkedInWhatsApp