SH

Process Management

Bash & Linux · 9 entries

List Running Processes

syntax
ps [options]
example
ps aux
ps aux | grep '[n]ginx'
ps -ef --forest
output
USER   PID %CPU %MEM    VSZ   RSS TTY  STAT START   TIME COMMAND
root     1  0.0  0.1 168940 11788 ?   Ss   Mar28   0:09 /sbin/init

Note aux shows all users' processes in BSD format. -ef is the System V equivalent. Wrapping one letter in brackets (e.g., '[n]ginx') in the grep pattern avoids matching the grep process itself in the results.

Interactive Process Viewer

syntax
top
htop
example
top -o %MEM
htop -u deploy

Note top is always available. In top: press M to sort by memory, P by CPU, k to kill a process, q to quit. htop is a friendlier alternative with mouse support, color, and tree view but may need installing. -u filters by user.

Send Signal to a Process

syntax
kill [signal] PID...
example
kill 28401
kill -9 28401
kill -HUP $(cat /var/run/nginx.pid)

Note Default signal is TERM (15), which asks the process to shut down gracefully. Use -9 (KILL) only as a last resort; it cannot be caught and may leave temp files or corrupt data. -HUP (1) tells many daemons to reload configuration.

Kill Processes by Name

syntax
killall [options] name
example
killall -TERM node
killall -u deploy python3

Note Matches by process name, not PID. -u restricts to a specific user's processes. WARNING: On Solaris, killall without arguments kills ALL processes, which is catastrophic. On Linux it requires a name argument and is safe.

Background, Foreground & Job List

syntax
command &
bg [%job]
fg [%job]
jobs
example
python3 train_model.py &
jobs
fg %1
output
[1]+  Running    python3 train_model.py &

Note Ctrl+Z suspends a foreground job. bg resumes it in the background. fg brings a background job to the foreground. jobs lists all jobs for the current shell session. %1 refers to job number 1.

Run Process After Logout

syntax
nohup command [args] &
example
nohup python3 etl_pipeline.py > etl.log 2>&1 &

Note nohup prevents the process from receiving SIGHUP when your shell exits. Output goes to nohup.out by default unless redirected. For more robust session persistence, consider tmux or screen.

Find or Kill Processes by Pattern

syntax
pgrep [options] pattern
pkill [options] pattern
example
pgrep -la nginx
pkill -f 'python3 worker.py'
pgrep -u deploy -c
output
14823 nginx: master process
14824 nginx: worker process

Note -l shows the process name alongside the PID. -f matches against the full command line, not just the process name. -u filters by user. pkill sends SIGTERM by default; add -9 for SIGKILL.

List Open Files & Ports

syntax
lsof [options]
example
lsof -i :3000
lsof -u deploy
lsof +D /var/log/
output
COMMAND   PID   USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
node    19283 deploy   22u  IPv4  284719      0t0  TCP *:3000 (LISTEN)

Note -i :port shows which process is using a port (essential for resolving 'address already in use' errors). -u filters by user. +D lists all open files within a directory. Requires root for other users' processes.

Wait for Background Jobs

syntax
wait [pid|%job]
example
process_a &
process_b &
wait
echo 'Both finished'

Note wait with no arguments paits for all background jobs. With a PID or job spec, it waits for just that one. Useful in scripts to parallelize tasks and then synchronize before continuing.