opscape

Processes and jobs cheat sheet

The most useful processes and jobs commands on one printable page, every line checked by running it.

bg Resume a stopped job in the background, as if it had been started with &.

set -m sleep 300 & kill -TSTP %1 sleep 0.1 jobs bg jobs
Resume a stopped job in the background output
set -m sleep 300 & kill -TSTP %1 sleep 0.1 bg %1 disown %1 jobs pgrep -a sleep
Background a job and let it survive logout output

fg Bring a background or stopped job into the foreground, so it gets the terminal and the shell waits for it.

set -m (sleep 1; echo "job finished") & kill -TSTP %1 sleep 0.1 jobs fg %1 echo "fg returned $?"
Resume a stopped job in the foreground output
set -m (sleep 0.5; exit 7) & fg echo "fg returned $?"
fg returns the job's exit status output

jobs List the background and stopped jobs of the current shell, with their job numbers and state.

sleep 300 & sleep 600 & jobs
List background jobs output
sleep 300 & sleep 600 | cat & jobs -l
Show the process IDs too output
sleep 300 & sleep 600 & jobs -p kill $(jobs -p) wait jobs -p | wc -l
Get just the PIDs for another command output
sleep 300 & python3 -c 'import time; time.sleep(600)' & jobs %1 jobs %python3 jobs %?600 jobs %-
Refer to a job by number, name or text output

kill Send a signal to processes by PID or job number; by default TERM, which asks a process to exit.

sleep 300 & kill $! wait $! echo "exit status: $?"
Ask a process to terminate output
kill -l 143 kill -l 137 kill -l HUP
Find out which signal killed a process output
bash -c 'trap "echo ignoring TERM" TERM; while :; do sleep 0.1; done' & sleep 0.3 kill $! sleep 0.3 kill -0 $! && echo "still running" kill -9 $! wait $! echo "exit status: $?"
Kill a process that will not exit output
sleep 300 & pid=$! if kill -0 "$pid" 2>/dev/null; then echo "$pid is running"; fi kill "$pid"; wait "$pid" kill -0 "$pid" 2>/dev/null || echo "$pid is gone"
Check whether a process is still alive output

killall Send a signal (TERM by default) to all processes running a program with the given name.

sleep 300 & sleep 600 & killall -v sleep
Stop every process with a given name output
killall nginx
Nothing to kill output
sleep 300 & sleep 301 & killall -v -HUP sleep
Send a different signal output
chmod +x slowstop.sh ./slowstop.sh & sleep 0.3 killall -w slowstop.sh echo "killall returned"
Wait until the processes have exited output

lsof List open files and the processes holding them, including network sockets, deleted files and working directories.

tail -f logs/app.log > /dev/null 2>&1 & sleep 0.2 lsof logs/app.log
Find which process has a file open output
python3 -m http.server 9000 > /dev/null 2>&1 & sleep 0.5 lsof -i :9000
Find what is listening on a port output
python3 -m http.server 9000 > /dev/null 2>&1 & python3 -m http.server 9001 --bind 127.0.0.1 > /dev/null 2>&1 & sleep 0.5 lsof -nP -i TCP -sTCP:LISTEN
List all listening TCP ports output
tail -f logs/app.log > /dev/null 2>&1 & (cd logs && sleep 300) & sleep 0.2 lsof +D logs
Find out what is using a directory output
head -c 5M /dev/zero > big.log tail -f big.log > /dev/null 2>&1 & sleep 0.2 rm big.log lsof -a +L1 -p $!
Find deleted files that still take up space output

nice Run a command with a lower (or, as root, higher) CPU scheduling priority, or print the current niceness.

nice
Print the current niceness output
nice -n 5 nice nice -n 19 nice nice -n 50 nice
Choose the adjustment output
nice -n 19 gzip -k -9 access.log & wait ls -l access.log*
Run a heavy job in the background without slowing the machine output

nohup Run a command immune to the hangup signal, so it keeps running after you log out or close the terminal.

nohup bash job.sh > job.log 2>&1 & echo "started job $!" wait cat job.log
Run a long job in the background with its own log output
bash -c 'kill -HUP $$; echo "survived"' echo "without nohup: exit status $?" nohup bash -c 'kill -HUP $$; echo "survived"' echo "with nohup: exit status $?"
See that the hangup signal is ignored output

pgrep Print the PIDs of processes whose name (or full command line) matches a pattern, with filters by user, parent and age.

sleep 300 & sleep 600 & pgrep sleep
Find the PIDs of a program output
sleep 300 & sleep 600 & pgrep -a sleep pgrep -l sleep
Show the command line next to each PID output
sleep 300 & pgrep -x slee; echo "slee: exit $?" pgrep -x sleep
Match the name exactly output
sleep 300 & if pgrep -x sleep >/dev/null; then echo "sleep is running"; fi pgrep -x nginx >/dev/null || echo "nginx is not running"
Test whether a program is running output
sleep 300 & pgrep -A -af 'sleep 300' kill %1
Exclude your own shell from -f matches output

pkill Send a signal (TERM by default) to every process whose name or command line matches a pattern.

sleep 300 & sleep 600 & pkill sleep wait pgrep sleep || echo "no sleep processes left"
Stop all processes with a given name output
sleep 300 & sleep 600 & pkill -e sleep
Report what was signaled output
sleep 300 & pkill -x slee; echo "slee: exit $?" pkill -e -x sleep
Match the exact process name output
chmod +x daemon.sh ./daemon.sh & sleep 0.3 pkill -HUP -x daemon.sh sleep 0.3
Tell a daemon to reload with HUP output
sleep 300 & pkill -e -A -f 'sleep 300' echo "still here"
Exclude your own shell with -A output

ps Print a snapshot of running processes, with the columns, filters and sort order you choose.

ps -ef
List every process on the system output
ps aux
List every process with CPU and memory use (BSD style) output
sleep 300 & ps -o pid,ppid,user,stat,etime,args kill $!
Choose exactly which columns to print output
sleep 300 & ps -o pid= -o etimes= -p $! echo "state: $(ps -o stat= -p $!)"
Get a bare value for a script output
ps -eo pid,user,rss,comm --sort=-rss | head -5
Find the processes using the most memory output

pstree Display running processes as a tree, showing which process started which.

(sleep 300 & sleep 301 & wait) & sleep 0.2 pstree kill %1
Show the whole process tree output
(sleep 300 & sleep 301 & wait) & sleep 0.2 pstree -p kill %1
Show PIDs output
sleep 300 & python3 -c 'import time; time.sleep(300)' & sleep 0.2 pstree -a $$ kill %1 %2
Show command lines output
(sleep 300 & wait) & sleep 0.2 pstree -s -p "$(pgrep -n -x sleep)" kill %1
Find out what started a process output

renice Change the niceness (CPU scheduling priority) of processes that are already running, by PID, process group or user.

sleep 300 & renice -n 10 -p $! ps -o pid,ni,args -p $!
Lower the priority of a running process output
sleep 300 & renice -n 5 -p $! renice --relative 3 -p $!
Add to the current value output
sleep 300 & sleep 301 & renice -n 12 -p $(pgrep -x sleep)
Renice every process of a program output

sleep Pause for a given amount of time, in seconds by default, with m, h and d suffixes and fractions allowed.

date +%T sleep 2 date +%T
Pause between two commands output
start=$(date +%s%N) sleep 0.25 echo "slept $(( ($(date +%s%N) - start) / 1000000 )) ms"
Sleep for a fraction of a second output
time sleep 0.5s 0.25 0.005m
Use units and add durations together output
for attempt in 1 2 3 4 5; do if [ -e ready.flag ]; then echo "ready after $attempt attempts"; break; fi echo "attempt $attempt: not ready, waiting" [ "$attempt" -eq 2 ] && touch ready.flag sleep 0.5 done
Retry a command with a delay output

time Measure how long a command takes (wall-clock, user and system CPU time); GNU /usr/bin/time also reports memory and I/O.

time sleep 0.5
Time a command output
TIMEFORMAT=%R { time sleep 0.2 > /dev/null; } 2> elapsed.txt echo "elapsed: $(cat elapsed.txt) s"
Capture the timing in a variable or file output
/usr/bin/time -v python3 -c 'x = bytearray(80_000_000)' 2>&1 | head -12
Full resource report with -v output
/usr/bin/time -f '%e s elapsed, %M KiB peak, exit %x' gzip -k -9 access.log
Pick the fields with -f output

timeout Run a command with a time limit, and kill it if it is still running when the time is up.

timeout 1 sleep 5 echo "exit status: $?"
Limit how long a command may run output
timeout 2 curl -s http://localhost:8080/api/status.json > status.json rc=$? if [ "$rc" -eq 124 ]; then echo "timed out" elif [ "$rc" -ne 0 ]; then echo "failed with $rc" else echo "ok: $(wc -c < status.json) bytes"; fi
Tell a timeout from a failure in a script output
timeout -k 1 1 bash -c 'trap "" TERM; while :; do sleep 0.1; done' echo "exit status: $?"
Kill a command that ignores TERM output
timeout --preserve-status 1 sleep 5 echo "exit status: $?"
Return the command's status even on timeout output

top Show a live, sorted view of processes and system load; batch mode prints snapshots for logs and scripts.

top -b -n 1 | tail -n +7
Print one snapshot of the process list output
python3 -c 'while True: pass' & sleep 1 top -b -n 1 -o %CPU | sed -n '7,10p'
Find the process that is burning CPU output
python3 -c 'while True: pass' & top -b -n 3 -d 0.5 -p $! | grep -A1 '^ *PID'
Watch specific processes over time output
python3 -c 'while True: pass' & top -b -n 3 -d 0.5 -p $! > top.log grep -c '^top -' top.log awk '$1 ~ /^[0-9]+$/ {print $9, $11}' top.log
Record snapshots to a file for later output

wait Wait for background jobs of the current shell to finish, and return their exit status.

for n in 3 1 2; do (sleep "0.$n"; echo "job $n done") & done wait echo "all finished"
Run jobs in parallel and wait for all of them output
(sleep 0.2; exit 3) & pid=$! echo "started $pid" wait "$pid" echo "job exited with $?"
Get a background job's exit status output
pids=() for f in data/users.csv data/missing.csv data/sales.csv; do wc -l "$f" > /dev/null 2>&1 & pids+=($!) done failed=0 for pid in "${pids[@]}"; do wait "$pid" || failed=$((failed + 1)) done echo "$failed job(s) failed"
Fail if any parallel job failed output
for n in 3 1 2; do (sleep "0.$n"; exit "$n") & done while :; do wait -n -p pid; rc=$? [ -n "$pid" ] || break echo "PID $pid finished with status $rc" done
React to jobs in the order they finish output

watch Rerun a command every few seconds and show its output full-screen, optionally highlighting changes or exiting when they happen.

watch -n 2 -d 'ls -l logs'
Rerun a command every two seconds output
(sleep 1; touch new-report.csv) & watch -g -n 0.2 ls > /dev/null echo "watch exit status: $?, the directory changed:" ls new-report.csv
Wait until a command's output changes output
(sleep 1; touch stop.flag) & watch -n 0.2 -e 'test ! -e stop.flag' > /dev/null echo "watch exit status: $?"
Stop when the command fails output