Guide Linux Intermediate

Process & Job Commands

ps, top, htop, kill, nice, and job control commands for inspecting and managing running processes on Linux.

3 min read

Viewing Running Processes

ps aux                        # every process on the system, BSD-style
ps -ef                          # every process, standard/POSIX-style
ps -ef --forest                   # tree view showing parent/child relationships
ps aux --sort=-%mem                 # sort by memory usage, descending
ps aux --sort=-%cpu                   # sort by CPU usage, descending
ps -p 1234                              # details for a specific PID
pgrep -f "python worker.py"               # find PIDs matching a command line pattern

Live Monitoring: top / htop

top                    # live, refreshing process view
top -o %MEM              # sort by memory
htop                       # nicer, colorized, scrollable version of top (if installed)

Inside top: press M to sort by memory, P for CPU, k to kill a PID, q to quit.

Killing & Signaling Processes

kill 1234                    # send SIGTERM (15) — graceful shutdown request
kill -9 1234                   # send SIGKILL — force kill, cannot be caught
kill -HUP 1234                   # send SIGHUP — often used to reload config
kill -l                            # list all available signals

pkill -f "node server.js"            # kill by matching command line, not PID
killall nginx                          # kill all processes by exact name

Job Control (Same Shell Session)

sleep 300 &          # run in background
jobs                    # list background jobs in this shell
fg %1                     # bring job 1 to foreground
bg %1                       # resume a stopped job in background
Ctrl+Z                        # suspend the current foreground job
disown %1                       # detach job so it survives shell exit
nohup ./long_task.sh &             # ignore SIGHUP, survives logout/disconnect

Priority: nice & renice

nice -n 10 ./cpu_heavy_task.sh    # start with LOWER priority (higher niceness = less priority)
nice -n -5 ./important_task.sh      # start with HIGHER priority (needs root for negative values)
renice -n 5 -p 1234                    # change priority of an already-running process

Niceness ranges from -20 (highest priority) to 19 (lowest) — the naming is intentionally backwards (“nicer” to other processes = lower priority for yourself).

Resource Usage Per Process

ps -p 1234 -o %cpu,%mem,etime,cmd    # CPU%, memory%, elapsed time, command
cat /proc/1234/status                   # detailed memory/state info straight from the kernel
lsof -p 1234                              # every file descriptor a process has open
strace -p 1234                              # trace live system calls (great for "why is it hanging")

Production Considerations

  • Prefer SIGTERM (default kill) before SIGKILL (kill -9) — it lets the application shut down cleanly (close DB connections, flush buffers).
  • pkill/killall matching by name is convenient but dangerous on shared hosts — a loosely matched pattern can kill unrelated processes; always verify with pgrep -f first.
  • High load average with low CPU usage often means processes stuck in uninterruptible I/O wait (D state in ps) — check disk/NFS health, not CPU.

Quick Interview Answer

ps aux/ps -ef give a snapshot of processes, top/htop give a live view, and kill/pkill send signals — SIGTERM for graceful shutdown, SIGKILL to force. Job control (&, jobs, fg, bg, Ctrl+Z) manages foreground/background execution within one shell session, and nice/renice adjust CPU scheduling priority, from -20 (highest) to 19 (lowest).”

Common Mistakes

  • Defaulting to kill -9 instead of trying a plain kill (SIGTERM) first.
  • Using killall/pkill with an overly broad pattern on a shared or production box.
  • Confusing “nice” priority (CPU scheduling) with process priority in the sense of startup order.

Add More Questions to This Guide

Know a question that should be here? Share it and help the community!

Open Google Form