Linucate
~ Linucate_

103.5 Create, monitor and kill processes

All Levels

Introduction

This lesson is about process control.

The main question is:

How do you start a process, move it to the background, watch it, and stop it safely?

What you should be able to do after this lesson:

  • Run jobs in the foreground and background.
  • Use job control.
  • Keep a process running after logout.
  • Monitor processes with common tools.
  • Send signals to processes.

Foreground and Background

Foreground

Normal behavior: the shell waits for the command to finish.

Background

Add & to run in the background:

sleep 300 &

Example output from sleep 60 & followed by jobs -l:

[1]+    13 Running                    sleep 60 &

What this shows:

  • the shell created background job number 1
  • the prompt returned while the process kept running

Job Control

Useful shell commands:

jobs
fg %1
bg %1

Simple idea:

  • jobs = list shell jobs
  • fg = bring a job to the foreground
  • bg = continue a stopped job in the background

Keep a Process Running After Logout

nohup

nohup long-command > out.log 2>&1 &

This helps a process survive logout.

Awareness of screen and tmux

These tools let you keep terminal sessions alive and reconnect later. LPIC expects awareness of them.

Simple difference:

  • nohup keeps one command running
  • screen and tmux keep an interactive terminal session alive

See Running Processes

Snapshot with ps

ps aux | head
ps -ef | head

Example output from ps -eo pid,comm,stat --sort=pid | head -n 8:

    PID COMMAND         STAT
      1 codex           Ss
      2 bash            S
      3 ps              R
      4 head            S

What this shows:

  • ps gives a process snapshot
  • STAT is a short process state indicator

Find by name

pgrep ssh
pgrep -a bash

Sort process display

ps -eo pid,comm,%mem,%cpu --sort=-%cpu | head
ps -eo pid,comm,ni --sort=ni | head

This helps when you need the "top CPU users" or "lowest niceness first" view without opening top.

Live monitor with top

top

Repeat a command every few seconds

watch free -h

Memory and Load Quick Checks

free -h
uptime

These are not full process lists, but they are useful health indicators.

Send Signals

Graceful stop

kill -15 <pid>

Force stop

kill -9 <pid>

By name

pkill ssh
killall bash

Simple rule:

  • try a gentle signal first
  • force kill only when needed

A Tiny Example

Start a background job:

sleep 300 &
jobs

Bring it back:

fg %1

Stop it with Ctrl+c or send a signal from another shell.

Practice Step by Step

1) Start and inspect a background job
  1. Run: sleep 120 &
  2. List jobs: jobs
  3. Find its PID: pgrep sleep
2) Stop a process safely
  1. Find the PID: pgrep -a sleep
  2. Stop gently: kill -15 <pid>
  3. Use kill -9 only if the process refuses to stop.

Cheat Sheet

  • & = run in background
  • jobs = list shell jobs
  • fg = foreground a job
  • bg = continue a stopped job in background
  • nohup = survive logout
  • ps = process snapshot
  • top = live process view
  • pgrep = find PIDs by pattern
  • pkill = signal processes by pattern
  • killall = signal by name
  • free -h = memory summary
  • uptime = load and uptime summary
  • watch = repeat a command
  • screen and tmux = persistent terminal sessions
🎯

Test Your Knowledge

Complete the quiz to assess your understanding of this course's concepts.