Skip to main content

Command Palette

Search for a command to run...

Process Management in Linux

Updated
•13 min read•View as Markdown
S
Software Engineer / Platform Engineer (DevSecops/SRE)

A process is defined as a running instance of a program, such as a Python application, a shell script, or a web server. Because software cannot interact directly with hardware, the operating system (OS) acts as an intermediary, managing CPU scheduling, memory, file systems, and networking. Linux kernel ensures every process gets its fair share of CPU time and memory.

Every process is identified by a unique Process ID (PID).

1.The Parent-Child Hierarchy

Every process in Linux is part of a family tree.

  • systemd (PID 1): The "ancestor" of all processes. It is the first process to start after the kernel boots.

  • Forking: When a process wants to start a new task, it "forks" itself to create a child process.

To understand the Parent-Child hierarchy, it helps to think of it as a chain of command. In Linux, no process is "homeless"—every single task can be traced back to its creator.

The Real-World Workflow

Imagine you open a Terminal and run the top command. Here is what happens behind the scenes:

  1. systemd (PID 1) is already running (it started when you turned on the computer).

  2. systemd forked itself to start your Display Manager (the login screen).

  3. Your Display Manager forked itself to start your Desktop Environment (GNOME/KDE).

  4. You clicked the terminal icon, so the Desktop Environment forked a Terminal process.

  5. Inside the terminal, you typed top. The Terminal forked a Shell (like Bash), and that Shell forked the top process.

Example Output: Tracing the Tree

The best way to see this is using the ps -f (full format) command. The -f flag adds the PPID (Parent Process ID) column, which acts as the "DNA link" to the creator.

If I run a simple command, the output looks like this:

UID          PID    PPID  C STIME TTY          TIME CMD
root           1       0  0 08:00 ?        00:00:02 /sbin/init
john        1205       1  0 09:15 ?        00:00:00 /usr/lib/gnome-terminal
john        1210    1205  0 09:15 pts/0    00:00:00 bash
john        1542    1210  0 10:30 pts/0    00:00:00 ps -f

Breaking Down the Output:

  • PID 1 (systemd/init): Notice its PPID is 0. This is the only process that has no "real" parent because it is the root of the tree.

  • PID 1205 (Terminal): Its PPID is 1. It was spawned directly by the system's main services.

  • PID 1210 (Bash): Its PPID is 1205. The Terminal started the Shell.

  • PID 1542 (ps -f): Its PPID is 1210. The Shell started the command you just typed.

Visualization with pstree

If the table is too messy, the pstree command gives you a visual "map" of the relationship.

Command: pstree -p -s 1542 (Show the parentage of our specific ps command)

Output:

systemd(1)───gnome-terminal-(1205)───bash(1210)───ps(1542)

Why does this matter?

  1. Environment Variables: Children inherit settings (like PATH or temporary variables) from their parents.

  2. Permissions: A child process usually can't do anything the parent isn't allowed to do.

  3. Termination: If you kill a parent (like closing the Terminal window), Linux will usually send a hangup signal to all the children (the Shell and any running commands) to shut them down together.

Does seeing that "PPID" column help clarify how the OS keeps track of who started what?


2. Process States

A process isn't always actively using the CPU. It cycles through several states depending on what it needs:

State Description
Running (R) The process is either currently using the CPU or is ready to run.
Sleeping (S/D) Waiting for an event (like a keystroke) or a resource (like a hard drive) to become available.
Stopped (T) The process has been suspended (usually by a user signal).
Zombie (Z) The process is finished, but its entry remains in the process table so the parent can read its exit code.

3. Resource Contention

When a process is resource-intensive (e.g., AI/ML calculations or multi-threaded Java applications), it can consume a disproportionate amount of CPU or memory, leading to "Out of Memory" (OOM) errors or system hangs. Administrators must be prepared to view, terminate, or prioritize these processes.

Key Process Commands

Command Description
ps show only current terminal processes
ps aux Lists all running processes with CPU and memory utilization.
ps -ef Lists processes (often used interchangeably with ps aux, but lacks memory metrics). It is often preferred for viewing the "PPID" (Parent Process ID) more clearly, which is vital for tracing your hierarchy tree.
`ps aux grep [name]`
`ps aux wc -l`

4. Process Life Cycle and Control

Administrators control processes by sending Signals. While the command is named kill, its primary job is to pass a specific instruction to a Process ID (PID).

  • Standard Termination (kill [PID]): Sends SIGTERM (Signal 15). This is the default and safest method. It allows the process to catch the signal, save its state, close file handles, and exit cleanly.

  • Forceful Termination (kill -9 [PID]): Sends SIGKILL (Signal 9). This cannot be ignored or blocked by the process. The kernel simply kills the process immediately. Warning: This can lead to data corruption as the process has no chance to "clean up."

  • Thread Dumps (kill -3 [PID]): Sends SIGQUIT (Signal 3). In Java environments, this triggers a dump of all current thread stacks to the standard output (usually the logs), which is vital for debugging "hung" applications without killing them.

  • Stop/Resume:

    • kill -STOP [PID] (Signal 19): Pauses a process instantly. The process remains in memory but uses 0% CPU.

    • kill -CONT [PID] (Signal 18): Resumes a process that was previously stopped.

  • Targeting by Name:

    • pkill [pattern]: Uses pattern matching. For example, pkill -9 python kills any process with "python" in its name.

    • killall [name]: Requires an exact match of the process name and kills every instance of it.

Common Management Signals

Every signal has a Name, a Number, and a Action. Understanding the numbers is useful for quick terminal work, but using the names is often considered better practice in scripts.

Signal Name Number Description Key Use Case
SIGHUP 1 Hangup Used to tell a daemon (like Nginx) to reload its configuration without stopping.
SIGINT 2 Interrupt Equivalent to pressing Ctrl+C. Gently stops a foreground task.
SIGQUIT 3 Quit Quits the process and produces a core dump for debugging.
SIGKILL 9 Kill The "Nuclear Option." Forces immediate termination; cannot be handled.
SIGTERM 15 Terminate The Polite Request. Standard way to ask a program to finish and exit.
SIGSTOP 19 Stop Suspends the process (cannot be ignored).
SIGCONT 18 Continue Resumes a process previously paused by SIGSTOP.

Pro-Tip for DevOps:

When troubleshooting a stubborn process, always try kill (SIGTERM) first. Wait a few seconds for the process to wrap up its I/O operations. Only if the process remains in the ps list after 10–15 seconds should you escalate to kill -9 (SIGKILL).


5. Process Scheduling: Nice Values vs. Priority

In Linux, the kernel's scheduler (the Completely Fair Scheduler or CFS) determines which process gets the CPU next. It uses two main values to decide this: Priority (PR) and Nice Value (NI).

Note:

  1. Only the root user (the administrator) can set negative nice values because

  2. CPU algorithms decide process priority based on time spent with the CPU.

The Relationship Formula

The kernel generally determines the actual priority using this logic: PR=20+NI

  • Nice Value (NI): A user-space value you can control.

  • Priority (PR): The actual internal priority used by the kernel.

The Nice Value Scale

The "Niceness" of a process is its willingness to be "nice" to other processes by giving up CPU time.

  • Range: -20 to 19.

  • Default Value: New processes usually start with a Nice value of 0.

  • Low Number (-20): High priority. The process is "not nice"; it grabs as much CPU time as possible.

  • High Number (19): Low priority. The process is "very nice"; it only uses the CPU when no one else needs it.

Manual Adjustment: nice vs. renice

Administrators use two different commands depending on whether the process is already running:

  1. nice (Starting a new process): Sets the priority at launch.

    • Example: nice -n 10 backup_script.sh (Starts the script with low priority).
  2. renice (Adjusting a running process): Changes the priority of an active PID.

    • Example: renice -n -5 -p 1234 (Increases the priority of PID 1234).

    • Caution: These commands should only be used with a deep understanding of the system, as they can impact critical operations.


Key Notes

**1.**only the root user (or a user with sudo privileges) can set a negative nice value. Regular users can only increase their nice value (make their own tasks lower priority), but they cannot decrease it or interfere with other users' processes.

2. Impact on Critical Operations :

If you set a non-critical, resource-heavy process to -20, it can "starve" essential system services of CPU cycles, causing the system to become unresponsive or laggy.

3. The "Priority" vs "Nice" Distinction

In tools like top or htop, you will see a PR column and an NI column. If you set a process to NI 5, you will see its PR change to 25 (\(20 + 5\)). If you see a PR value of rt, that means it is a Real-Time process, which operates on a different scale entirely (0 to 99) and always stays ahead of "Nice" values.


Summary Table: Process Scheduling: Nice Values vs. Priority

Attribute High Priority Low Priority
Nice Value -20 (Least Nice) 19 (Most Nice)
Kernel Priority 0 39
User Access Root only All users
Common Use Database engines, X-Server Background backups, Compiling code

6. Services vs. Processes

Services are specialized background processes that start automatically during the In Linux and Unix-like systems, the distinction between a process and a service (often called a daemon) is fundamental to how the operating system manages resources and availability.

While every service is a process, not every process is a service.


1. Defining the Process

A process is simply an instance of a running program. It is the basic unit of work in an operating system.

  • Lifecycle: It starts when a user or another program executes a command and usually ends when the task is finished or the user closes the application.

  • Interaction: Most processes are "foreground" tasks (like a text editor or a terminal command) that are directly tied to a user session.

  • Persistence: If the system reboots or the user logs out, the process typically terminates and does not restart on its own.

2. Defining the Service (Daemon)

A service is a specialized type of process that runs in the background, independent of any specific user session.

  • Lifecycle: Services are designed to be long-running. They often start during the system boot sequence and stay active until the system shuts down.

  • Persistence: They are configured to restart automatically if they crash or if the server reboots. This is why web servers (Nginx), database engines (MySQL), and SSH listeners are services—they must be "always on" to respond to requests.

  • Naming Convention: In Linux, services are frequently called daemons, which is why many service names end in "d" (e.g., sshd, httpd, systemd).


3. Key Differences at a Glance

Feature Process Service (Daemon)
User Interaction Directly managed by a user. Runs silently in the background.
Start Time Started manually/on-demand. Starts at boot or via a manager.
Termination Ends when the task is done. Persists until explicitly stopped.
Parentage Child of a shell or desktop environment. Child of the Init system (PID 1).

4. Management Utility: systemctl

Modern Linux distributions use systemd as the init system to manage services. The primary tool for interacting with systemd is systemctl.

Identifying Services

To see what is currently running on your system, you can filter the units:

  • systemctl list-units --type=service Displays all active service units currently loaded into memory.

Controlling Service States

You use specific sub-commands to change how a service behaves in real-time or across reboots:

  • systemctl start [service]: Launches a service immediately.

  • systemctl stop [service]: Gracefully shuts down a running service.

  • systemctl restart [service]: Stops and then starts the service (useful for applying configuration changes).

  • systemctl status [service]: Provides a "health check," showing if the service is active, its Process ID (PID), and recent log entries.

Ensuring Persistence

The true power of a service lies in its Enable/Disable state:

  • systemctl enable [service]: Configures the service to start automatically every time the system boots.

  • systemctl disable [service]: Prevents the service from starting at boot, though it can still be started manually.

Note:

  1. A service can be active (running right now) but disabled (won't start after a reboot), or inactive (stopped now) but enabled (will start automatically next time).

  2. Management Utility Quick-Check:

Your systemctl commands are exactly what you'll use 99% of the time. If you ever need to troubleshoot why a service failed to start, the companion command to systemctl status is usually: journalctl -u [service-name]

This allows you to see the full historical logs specifically for that service.

7. System Monitoring

Monitoring involves tracking CPU, memory, and disk utilization to identify bottlenecks.

CPU and Memory Monitoring

Tool Functionality
top Provides real-time, updating views of CPU and memory usage by process.
htop A visual wrapper for top offering a more intuitive, color-coded interface.
vmstat Reports system performance and memory caching.
free -h Displays total, used, and available memory in a human-readable format.
nproc Shows the number of CPUs available to the system.

Disk Monitoring

Administrators must monitor disk space to prevent system failure due to log accumulation or software updates.

  • df -h: Shows disk utilization across different file system partitions.

  • du -sh [folder]: Summarizes the disk usage of a specific directory, useful for identifying large log files.

Modern Observability

While command-line tools are vital for quick troubleshooting, enterprise-level monitoring often involves integrating Linux servers with tools like Prometheus (for scraping metrics) and Grafana (for visualization and alerting). This allows for automated notifications via email or Slack when resource thresholds are exceeded.

More from this blog

Sruthi Palle's blog

36 posts