🚀 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):
systemctlstatusnginx
What Happens?
Shows active or failed status, logs, and recent errors.
🔹 List all running services:
systemctllist-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.