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 jobsfg= bring a job to the foregroundbg= 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:
nohupkeeps one command runningscreenandtmuxkeep 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:
psgives a process snapshotSTATis 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
- Run:
sleep 120 & - List jobs:
jobs - Find its PID:
pgrep sleep
2) Stop a process safely
- Find the PID:
pgrep -a sleep - Stop gently:
kill -15 <pid> - Use
kill -9only if the process refuses to stop.
Cheat Sheet
&= run in backgroundjobs= list shell jobsfg= foreground a jobbg= continue a stopped job in backgroundnohup= survive logoutps= process snapshottop= live process viewpgrep= find PIDs by patternpkill= signal processes by patternkillall= signal by namefree -h= memory summaryuptime= load and uptime summarywatch= repeat a commandscreenandtmux= persistent terminal sessions
Test Your Knowledge
Complete the quiz to assess your understanding of this course's concepts.
