What is a process?
Whenever you run a program, the operating system creates a process for it. Processes fall into two broad categories:- Short-lived commands: utilities like
ls,cp, or small scripts that run and exit quickly. - Long-lived services (daemons): system services like
sshdthat typically run continuously in the background.
ls spawns a short-lived process that lists directory contents and then exits:
Inspect running processes with ps
The primary tool for listing processes isps. Important to note: ps supports two option styles that are not identical:
- Unix/POSIX-style options use a leading dash (e.g.,
-A,-e). - BSD-style options do not use a leading dash (e.g.,
a,x,u).
ps with no options shows processes associated with the current terminal/session:
ps aux, which lists every process with human-friendly columns:
Common
ps option examples:
Inspect files a process has open with lsof
To see which files, sockets, or directories a process has open, uselsof (list open files). Use -p to specify a PID:
root), you will often need elevated privileges to get the full output. Prefix with sudo when necessary:
lsof may warn about incomplete output for certain filesystems (for example, FUSE filesystems used by virtual file systems). Example:
Terminate (kill) processes
When you need to stop a process, prefer graceful methods first, then escalate if necessary.
Examples:
- Try
kill <PID>(SIGTERM) first so the process can clean up. - Use
kill -9 <PID>(SIGKILL) only when the process is unresponsive. - Be careful when using
pkillorkillallon production systems — they can affect multiple processes.
Remember that
ps accepts both BSD-style (no leading dash) and Unix/POSIX-style (leading dash) options. They are not interchangeable — check man ps if behavior seems unexpected.Troubleshooting tips
- If
psoutput is missing columns or looks different, you may be mixing BSD and Unixpsoptions. Consistently useps auxorps -efdepending on your needs. - When
lsofreports permission errors or incomplete output, re-run withsudoto inspect system processes and protected files. - Use
toporhtopfor real-time, interactive process monitoring and sorting by CPU or memory.
Summary
- Processes are created when programs run; short-lived commands exit quickly while daemons run long-term.
psis the standard tool to list processes.ps auxis a common BSD-style view;ps -efis the Unix-style equivalent.- Use
lsof -p \<PID>`to list files a process has open; run withsudo` to see processes owned by other users. - Terminate processes with
killfor a graceful stop, andkill -9only if necessary. Usepkillorkillallwhen matching by name or pattern.
Links and references
- ps(1) manual — man7.org
- lsof(8) manual — linux.die.net
- sudo(8) manual — linux.die.net
- Kubernetes Documentation (for process concepts in containerized environments)