Processes and job control

Learning objectives

Identify a process, distinguish foreground and background jobs, inspect resource use, and stop only a verified process safely.

1 Objective→ 2 Learn→ 3 Example→ 4 Practice→ 5 Verify→ 6 Continue
How to study this lessonUnderstand the idea first. Use the example to see the behaviour, practise where provided, then make sure you can explain the result before continuing.

Explanation

Processes and jobs

A process is a running program with a process ID, called a PID. A job is a command managed by the current shell. A foreground job uses the terminal until it finishes; a background job lets the shell accept another command.

Commands you will use

Command What it does Example
ps Shows a snapshot of processes. ps -ef
pgrep Finds PIDs and commands by name. pgrep -a bash
top Shows changing CPU and memory use. top
jobs Lists jobs started by the current shell. jobs -l
bg / fg Continues a stopped job in the background or foreground. fg %1
kill Sends a signal to a PID. kill -TERM 1234
nohup Keeps a command running after logout when used correctly. nohup command > run.log 2>&1 &
wait Waits for a background job and returns its status. wait "$pid"
time Reports command runtime. time ./run_sim.sh
timeout Stops a command after a time limit. timeout 30s ./test.sh

Try SIGTERM first because it allows cleanup. Use SIGKILL only when a confirmed process does not respond. Always check the PID, owner, and command before sending a signal.

Worked example

sleep 30 &
lab_pid=$!
printf 'started pid=%s\n' "$lab_pid"
jobs -l
ps -p "$lab_pid" -o pid,ppid,stat,etime,cmd
kill -TERM "$lab_pid"
wait "$lab_pid"
printf 'wait-status=%s\n' "$?"

Expected result: ps shows the sleep process, kill -TERM requests termination, and wait returns a nonzero status because the process ended from a signal.

RTL/DV use

Long regressions are commonly started with logging and later inspected by PID. Keep the PID and log path together; do not use a broad pkill pattern on a shared server.

Common mistakes

  • Killing a PID without checking its owner and command.
  • Using kill -9 as the first action.
  • Assuming & alone makes a job survive logout.
  • Starting a regression without redirecting its output to a known log.

Check yourself

Why should SIGTERM be tried before SIGKILL?

Engineering practice

Practice: Processes and job control

Task→Run / reason→Check result→Explain why

Run a short sleep in the foreground and background. Inspect it with jobs, ps, and pgrep, then stop only your background process with SIGTERM.

Practice work is non-certified and is not stored by ASIC Campus.