Processes & Jobs
Every program you start becomes a process: a running copy of the program with its own memory, a numeric process ID (PID), an owner, and a parent (usually the shell that launched it). This lesson shows how to stop a stuck command, list and end processes, run commands in the background, and read the exit code a process leaves behind, on all four shells.
Terminal app
└─ shell (cmd.exe / pwsh / bash / zsh) PID 4120
├─ python train.py (foreground) PID 4388
└─ npm run dev (background job) PID 4391Stopping the command in front of you
| Keys | Windows (CMD, PowerShell) | Linux & macOS (bash/zsh) |
|---|---|---|
Ctrl+C |
interrupt the running command | send SIGINT: interrupt the running command |
Ctrl+Break |
harder interrupt for console programs | (no equivalent) |
Ctrl+Z |
(in CMD: types ^Z, end-of-file) |
send SIGTSTP: pause the job, back to the prompt |
Ctrl+D |
(no effect in CMD) | end of input; on an empty prompt it exits the shell |
Ctrl+C is the one to remember: it works everywhere. In CMD, stopping a batch file asks Terminate batch job (Y/N)?.
Listing processes
Windows (CMD)
tasklist lists every process with its PID and memory use. Filter with /fi:
C:\Users\ada>tasklist /fi "imagename eq notepad.exe"
Image Name PID Session Name Session# Mem Usage
========================= ======== ================ =========== ============
Notepad.exe 11872 Console 1 41,284 KWindows (PowerShell)
Get-Process (alias ps) returns objects, so you can sort and filter them with the pipeline:
PS C:\Users\ada> Get-Process | Sort-Object WorkingSet64 -Descending | Select-Object -First 3 Name, Id, @{n='MB'; e={[int]($_.WorkingSet64 / 1MB)}}
Name Id MB
---- -- --
Code 15220 612
msedge 9904 431
pwsh 14108 118(Sample output — yours will differ.) The same cmdlet works in PowerShell 7 on macOS and Linux.
Linux & macOS (bash/zsh)
ps shows processes; ps aux shows all of them, and pgrep finds PIDs by name. For a live, updating view use top (press q to quit), or the friendlier htop if it's installed:
ada@ubuntu:~$ ps aux | head -n 3
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 23412 13804 ? Ss 09:02 0:01 /sbin/init
root 2 0.0 0.0 0 0 ? S 09:02 0:00 [kthreadd]
ada@ubuntu:~$ pgrep -l python
5521 python3macOS's ps and top are the BSD versions: ps aux works, but top sorts and accepts options differently (top -o cpu). On Windows, the graphical Task Manager (Ctrl+Shift+Esc) and on macOS Activity Monitor show the same information.
Ending a process
| Task | CMD | PowerShell | bash/zsh |
|---|---|---|---|
| by PID | taskkill /pid 11872 |
Stop-Process -Id 11872 |
kill 5521 |
| by name | taskkill /im notepad.exe |
Stop-Process -Name notepad |
pkill notepad |
| force | taskkill /f /pid 11872 |
Stop-Process -Id 11872 -Force |
kill -9 5521 |
C:\Users\ada>taskkill /im notepad.exe
SUCCESS: Sent termination signal to the process "Notepad.exe" with PID 11872.On Linux and macOS, kill sends a signal. The default, SIGTERM (15), politely asks the program to clean up and exit. kill -9 (SIGKILL) cannot be ignored, but the program gets no chance to save anything, so try the polite version first. On Windows, taskkill without /f similarly asks the window to close. You can only end your own processes unless you are an administrator or use sudo.
Background jobs
A shell can start a command and give you the prompt back immediately. In bash and zsh you add &; jobs lists them and wait blocks until they finish. Scripts use this to do several things at once:
slow_task() {
sleep "$1"
echo "task $2 done after ${1}s"
}
slow_task 0.3 A &
slow_task 0.1 B &
echo "both started, the prompt is free"
jobs | wc -l
wait
echo "all background jobs finished"both started, the prompt is free
2
task B done after 0.1s
task A done after 0.3s
all background jobs finished
Task B finishes first because it sleeps less, even though it was started second. Interactively, Ctrl+Z pauses a foreground job, bg continues it in the background and fg brings it back. zsh behaves the same; it just announces finished jobs in a slightly different format.
PowerShell has jobs too. Start-Job runs a script block in a separate process; Start-ThreadJob is lighter and uses a thread. Receive-Job -Wait collects the output:
$a = Start-ThreadJob { Start-Sleep -Milliseconds 300; "job A done" }
$b = Start-ThreadJob { Start-Sleep -Milliseconds 100; "job B done" }
"both started"
$b, $a | Receive-Job -Wait -AutoRemoveJob
"all jobs finished"both started
job B done
job A done
all jobs finished
CMD has no job control. start "" notepad.exe opens a program without waiting for it, and start /b command runs a console command in the background of the same window, but there is no jobs or wait.
Exit codes
When a process ends it returns a number to its parent: 0 means success, anything else means failure. Scripts read it to decide what to do next.
| Shell | Last exit code | Example |
|---|---|---|
| CMD | %ERRORLEVEL% |
if errorlevel 1 echo failed |
| PowerShell | $LASTEXITCODE (programs), $? (True/False) |
if (-not $?) { 'failed' } |
| bash/zsh | $? |
if [ $? -ne 0 ]; then echo failed; fi |
mkdir -p demo
ls demo > /dev/null
echo "ls on an existing folder: $?"
ls missing-folder 2> /dev/null
echo "ls on a missing folder: $?"
grep -q needle <<< "haystack"
echo "grep found nothing: $?"ls on an existing folder: 0
ls on a missing folder: 2
grep found nothing: 1
@echo off
mkdir demo
dir /b /o:n demo >nul
echo dir on an existing folder: %ERRORLEVEL%
cmd /c exit 3
echo a command that exits with 3: %ERRORLEVEL%
findstr needle nul >nul
echo findstr found nothing: %ERRORLEVEL%dir on an existing folder: 0
a command that exits with 3: 3
findstr found nothing: 1
New-Item -ItemType Directory demo | Out-Null
Get-ChildItem demo | Out-Null
"cmdlet succeeded: $?"
Get-ChildItem missing-folder -ErrorAction SilentlyContinue
"cmdlet failed: $?"
pwsh -NoProfile -Command 'exit 3'
"program exit code: $LASTEXITCODE"cmdlet succeeded: True
cmdlet failed: False
program exit code: 3
Notice the PowerShell difference: $? is only True/False, while the numeric code of an external program lands in $LASTEXITCODE.
Learn more: Bash Jobs, CMD Processes, Processes (OS internals), Signals.
Test yourself
Which key combination interrupts the running command in all four shells?
- Ctrl+Z
- Ctrl+C
- Ctrl+D
- Ctrl+Q
What does kill 5521 send by default on Linux?
- SIGKILL
- SIGTERM
- SIGSTOP
- SIGINT
In PowerShell, where is the numeric exit code of an external program stored?
$?%ERRORLEVEL%$LASTEXITCODE$EXIT