Linux course index · Linux Basics category · Lesson 7 of 8
AI disclosure — AI-generated content, posted as is. AI can make mistakes. This lesson draws on the linked Open Education posts and adds original explanations or lab examples where useful. The newly written explanations and examples may be freely reused. Linked source material retains its own terms.
Learning goal
Identify a process you own, inspect its state, and stop it gracefully.
Concepts and setup
A program is code. A process is a running instance with a process ID (PID). A shell job is the shell’s record of one or more processes started together. top refreshes a system view, while ps shows a snapshot. Use a specific PID rather than a broad name match when acting on a process.
SIGTERM requests termination and can be caught by a process. SIGKILL is not catchable, so it prevents normal cleanup. The older source lists both. This lab starts and terminates only its own sleep process.
Start a job and verify termination
sleep 120 &
pid=$!
jobs -l
ps -p "$pid" -o pid,ppid,stat,comm
kill -TERM "$pid"
wait "$pid"
status=$?
printf 'wait status: %s\n' "$status"
ps -p "$pid" -o pid= || printf 'process no longer listed\n'
Possible output
[1]+ 3184 Running sleep 120 &
PID PPID STAT COMMAND
3184 3142 S sleep
wait status: 143
process no longer listed
Run interactively in Bash. PID and job notifications vary. The common Bash status 143 is 128 plus signal number 15. wait handles the shell’s own child.
Inspect the shell’s process context
ps -p "$$" -o pid,ppid,stat,comm
uptime
Possible output
PID PPID STAT COMMAND
3142 3072 Ss bash
14:09:38 up 2:12, 1 user, load average: 0.07, 0.04, 0.01
Use the same shell when comparing its PID to the child’s PPID. Load average is not a simple percentage of CPU use.
Questions only
Quick-check questions
- What does $! contain after sleep 120 &?
- Why try SIGTERM before SIGKILL?
Assessment questions
- Name two fields from ps useful for checking the target before a signal.
- What happens if you use wait for a PID that is not a child of this shell?
- Explain why jobs in a new terminal does not list the old terminal’s sleep job.
Questions and answers
Quick-check answers
- What does $! contain after sleep 120 &?
Answer — It contains the PID of the most recent background job’s last process, which is sleep here. - Why try SIGTERM before SIGKILL?
Answer — The process can handle SIGTERM and clean up. It cannot catch SIGKILL.
Assessment answer key
- Name two fields from ps useful for checking the target before a signal.
Answer — PID and command name are a start. Verify owner and parent as needed. - What happens if you use wait for a PID that is not a child of this shell?
Answer — Bash normally reports that it is not a child and returns 127. - Explain why jobs in a new terminal does not list the old terminal’s sleep job.
Answer — The job table belongs to the shell that created the job.
Source articles on this site
- Linux ps command
- Terminating Process in Linux
- Process Control in Linux
- Monitor Process in Linux
- ইউনিক্স প্রসেস ম্যানেজমেন্ট (Unix – Processes Management)
Continue the course
Previous lesson 06 — File Permissions and umask. Next lesson 08 — Archives, Compression, and a Restore Test.
Linux course index · Linux Basics category · Lesson 7 of 8
