Bash 5 reference · Text and System Tools
ps and kill (Linux command)
List processes with ps and send signals with kill: SIGTERM to ask a process to stop, SIGKILL only as a last resort.
Bash 5
Syntax
ps [aux|-ef|-o FIELDS -p PID] kill [-SIGNAL] PID pgrep / pkill PATTERN
Parameters
| Parameter | Description |
|---|---|
ps -o pid,args -p PID |
Chosen columns for one process. |
kill PID |
Send SIGTERM. |
kill -9 PID |
SIGKILL: no cleanup. |
kill -0 PID |
Check whether a process exists. |
pkill -P PID |
Only children of PID: safer than matching with -f. |
Examples
sleep 30 &
pid=$!
ps -o args= -p "$pid"
kill "$pid"; wait "$pid" 2>/dev/null; echo "exit status $? (143 = SIGTERM)"
Output
sleep 30 exit status 143 (143 = SIGTERM)
Run this on your computer — see “Get Started” for setup.
sleep 60 & sleep 60 &
pgrep -c -P $$ sleep
pkill -P $$ sleep; wait 2>/dev/null; echo stopped
Output
2 stopped
Run this on your computer — see “Get Started” for setup.
Related
trap· Run commands when the script exits or receives a signal; the standard way to clean up temporary files and child processes.- Bash Processes and Jobs · tutorial