Service & Process Management

🚀 Linux Service & Process Management (Complete Guide)

This guide covers service and process management in Linux, including how to start, stop, restart, monitor, and troubleshoot services and processes with real-world scenarios and solutions.

PROCESS STATES

  • Zombie: has completed execution, still has an entry in the process table

  • Orphan: parent has finished or terminated while this process is still running

  • Daemon: runs as a background process, not under the direct control of an interactive user


🔹 Step 1: Service Management (Systemd & SysVinit)

1.1 Checking the Status of Services

🔹 Check if a service is running (e.g., nginx):

systemctl status nginx

What Happens?

  • Shows active or failed status, logs, and recent errors.

🔹 List all running services:

systemctl list-units --type=service --state=running

What Happens?

  • Displays all active services in the system.

🔹 Check failed services:

What Happens?

  • Lists services that have failed to start.

Common Issue: Service Not Running 🔹 Check logs for failure reason:


1.2 Starting, Stopping & Restarting Services

🔹 Start a service:

What Happens?

  • Launches nginx service.

🔹 Stop a service:

What Happens?

  • Stops nginx immediately.

🔹 Restart a service (apply changes):

What Happens?

  • Stops and starts nginx, applying new configs.

🔹 Reload service without stopping:

What Happens?

  • Reloads nginx configuration without downtime.

Common Issue: Service Doesn't Start 🔹 Check configuration for syntax errors:


1.3 Enable & Disable Services (Boot Startup)

🔹 Enable service to start on boot:

What Happens?

  • Creates a symlink, ensuring service runs on boot.

🔹 Disable a service from starting at boot:

What Happens?

  • Removes boot startup link for the service.

🔹 Check if a service is enabled on boot:

Common Issue: Service Doesn't Start on Boot 🔹 Re-enable service and reboot:


🔹 Step 2: Process Management in Linux

2.1 Viewing Running Processes

🔹 View all running processes:

🔹 View processes by CPU & memory usage:

🔹 Interactive process monitor (better than top):

🔹 Find a specific process (e.g., nginx):

Common Issue: Process Not Showing 🔹 Check if process is running:

🔹 If not running, restart the service:


2.2 Killing Processes

🔹 Kill a process by PID:

What Happens?

  • -9 sends SIGKILL, forcefully stopping the process.

🔹 Kill all processes of a program (e.g., nginx):

🔹 Kill process by name:

Common Issue: Process Keeps Restarting 🔹 Check if the service is enabled in Systemd:

🔹 Disable it if not needed:


2.3 Managing Background & Foreground Processes

🔹 Run a command in the background:

What Happens?

  • Runs ping in the background, saving output to ping.log.

🔹 List background jobs:

🔹 Bring background job to foreground:

What Happens?

  • Brings Job #1 to the foreground.

🔹 Move a process to background:

Common Issue: Lost Terminal, But Process Still Running 🔹 Find and reattach process:

🔹 Use screen or tmux to keep processes running after logout.


🔹 Step 3: Monitoring & Troubleshooting Services & Processes

3.1 Checking System Load

🔹 Check system load & uptime:

🔹 Check system load with CPU/memory details:

Common Issue: High CPU Usage 🔹 Find the process consuming most CPU:

🔹 Kill high CPU process:


3.2 Checking Service Logs & Debugging Issues

🔹 Check logs for a failed service:

🔹 Check why a service failed:

🔹 Reinstall a service if it's corrupted:

Common Issue: Port Already in Use 🔹 Find which process is using a port (e.g., 80):

🔹 Kill the conflicting process:


🚀 Summary

Started, stopped, restarted services (systemctl start/stop/restart) ✔ Enabled/disabled services on boot (systemctl enable/disable) ✔ Monitored processes (ps, top, htop) ✔ Killed unwanted processes (kill, pkill, killall) ✔ Checked logs & fixed issues (journalctl, netstat)


Next: Do you want to cover automation of service management using Ansible or Bash scripts? 🚀

1️⃣ What is a Memory Leak?

A memory leak happens when a program keeps consuming memory but never releases it, eventually causing high memory usage and system slowdown.

Example Scenario:

  • A Python or Java application keeps allocating memory (e.g., storing large objects in memory)

  • It forgets to release unused memory, even after objects are no longer needed

  • The system runs out of RAM, causing performance issues or crashes

🔍 How to Detect a Memory Leak?

  • Check memory usage:

  • Find the process using the most RAM:

  • Check real-time memory leaks:

🔥 How to Fix a Memory Leak?

  • Restart the leaking service:

  • Use a memory profiler (Python example):


2️⃣ What is a Zombie Process?

A zombie process is a process that has finished running but is still listed in the process table because its parent process hasn’t cleaned it up.

Example Scenario:

  • A child process finishes execution but is not properly removed by its parent

  • The OS marks it as "defunct" (zombie)

  • Too many zombies waste process slots and slow down the system

🔍 How to Detect Zombie Processes?

  • List zombie processes:

  • Find parent process (PPID):

🔥 How to Fix Zombie Processes? 1️⃣ Restart the parent process:

2️⃣ Manually remove orphaned zombies:

3️⃣ If all else fails, reboot the system:


🚀 Key Differences:

Feature
Memory Leak
Zombie Process

Cause

Process keeps consuming memory without releasing it

Child process is not properly removed by its parent

Effect

High RAM usage, system slowdown, possible crashes

Wasted process table slots, too many can degrade performance

Fix

Restart the leaking service, use memory profiling tools

Restart the parent process, manually kill zombies

Detection

top, htop, ps aux --sort=-%mem

`ps aux

Would you like help debugging a memory leak in a real application? 🚀

🔍 ps vs top – Process Monitoring in Linux

Both ps (Process Status) and top (Task Manager-like Process Viewer) are used to monitor running processes, but they serve different purposes.


1️⃣ ps (Process Status)

📌 Used for: Getting a static snapshot of running processes. 📌 Best for: Checking specific processes, sorting them, and debugging issues.

Example Usage:

  • List all running processes

  • Find a specific process (e.g., Nginx)

  • Sort processes by memory usage

  • Sort by CPU usage

📌 Key Features: ✔️ Shows a snapshot (not real-time) ✔️ Useful for scripting and debugging ✔️ Can be filtered and sorted easily


2️⃣ top (Task Manager for Linux)

📌 Used for: Real-time monitoring of CPU, memory, and system load. 📌 Best for: Finding resource-hungry processes & troubleshooting performance issues.

Example Usage:

  • Start top command

  • Sort by memory usage (press Shift + M)

  • Sort by CPU usage (press Shift + P)

  • Kill a process from top (press k, then enter PID)

  • Quit top (press q)

📌 Key Features: ✔️ Real-time monitoring ✔️ Interactive (can sort & kill processes) ✔️ Good for diagnosing CPU & memory spikes

Last updated