Skip to content

Managing Linux Processes and Services ​

A process is an instance of a program that is being executed. Understanding how to monitor, manage, and control processes is essential for Linux administration and troubleshooting.

Process Basics ​

Process States ​

Linux processes can exist in several states:

  • Running: Currently executing or ready to run
  • Sleeping: Waiting for a resource or event
  • Stopped: Process has been suspended
  • Zombie: Process has completed but still has an entry in the process table
  • Dead: Process is being terminated

Process Attributes ​

Every process has:

  • PID (Process ID): Unique identifier for the process
  • PPID (Parent Process ID): ID of the parent process that spawned it
  • UID/GID: User/Group ID of the process owner
  • Priority/Nice value: Determines CPU scheduling priority
  • Terminal: The controlling terminal (if any)
  • Status: Current state of the process
  • Command: The command that started the process

Viewing Processes ​

ps - Process Status ​

bash
# Basic process information for current terminal
ps

# All processes with detailed information
ps aux

# Display process hierarchy
ps -ef

# Show processes for a specific user
ps -u username

# Custom format (PID, user, command, memory, CPU)
ps -eo pid,user,cmd,%mem,%cpu

top - Interactive Process Viewer ​

bash
top

Interactive commands within top:

  • h: Display help
  • k: Kill a process (enter PID)
  • r: Renice a process (change priority)
  • q: Quit
  • M: Sort by memory usage
  • P: Sort by CPU usage
  • u: Filter by user

htop - Enhanced top ​

bash
# Install if not already available
sudo apt install htop  # Debian/Ubuntu
sudo dnf install htop  # Fedora/RHEL

# Run htop
htop

htop provides a more user-friendly interface with color highlighting and mouse support.

Process Control ​

Starting Processes ​

bash
# Run program in foreground
program_name

# Run program in background
program_name &

# Run program with different priority (nice value)
nice -n 10 program_name  # Higher value = lower priority

Controlling Running Processes ​

bash
# Send process to background
Ctrl+Z (to suspend)
bg (to continue in background)

# Bring background process to foreground
fg

# List background jobs
jobs

Terminating Processes ​

bash
# Gracefully terminate a process
kill PID

# Force kill a process
kill -9 PID

# Kill by process name
killall process_name

# Kill all processes owned by a user
pkill -u username

Common kill signals: ​

SignalNumberPurpose
SIGHUP1Hangup, often used to reload configuration
SIGINT2Interrupt (same as Ctrl+C)
SIGTERM15Terminate gracefully (default signal)
SIGKILL9Kill immediately (cannot be ignored)
SIGSTOP19Stop process (cannot be ignored)

Process Monitoring ​

Process Resource Usage ​

bash
# Check CPU and memory usage of processes
top
htop

# Show memory usage
free -h

# Check specific process resource usage
ps -p PID -o %cpu,%mem,cmd

System Load Average ​

bash
# View system load averages for 1, 5, and 15 minutes
uptime

# More detailed view
cat /proc/loadavg

Process Resource Limits ​

bash
# View resource limits for current shell
ulimit -a

# Set resource limits (example: max file size)
ulimit -f 1000  # Limit file size to 1000 blocks

System Services and Daemons ​

Using systemd (Modern Linux distributions) ​

bash
# Start a service
sudo systemctl start service_name

# Stop a service
sudo systemctl stop service_name

# Restart a service
sudo systemctl restart service_name

# Enable service to start at boot
sudo systemctl enable service_name

# Disable service from starting at boot
sudo systemctl disable service_name

# Check service status
sudo systemctl status service_name

# List all services
systemctl list-units --type=service

Using SysVinit (Older Linux distributions) ​

bash
# Start a service
sudo service service_name start

# Stop a service
sudo service service_name stop

# Restart a service
sudo service service_name restart

# Check service status
sudo service service_name status

# List all services
service --status-all

Scheduling Tasks ​

Using cron ​

bash
# Edit current user's crontab
crontab -e

# List current user's crontab entries
crontab -l

# Edit another user's crontab (as root)
sudo crontab -u username -e

Crontab syntax: minute hour day_of_month month day_of_week command

Example entries:

# Run at 2:30am every day
30 2 * * * /path/to/script.sh

# Run every hour
0 * * * * /path/to/script.sh

# Run every Monday at 9am
0 9 * * 1 /path/to/script.sh

Using at for one-time tasks ​

bash
# Run command at specified time
at 10:00 AM
> command1
> command2
> Ctrl+D

# List pending jobs
atq

# Remove a job
atrm job_number

Background Processes ​

nohup - Run Command Immune to Hangups ​

bash
# Run a command that continues after logout
nohup command &

# Output is saved to nohup.out by default
nohup command > output.log 2>&1 &

screen and tmux - Terminal Multiplexers ​

bash
# Install if not already available
sudo apt install screen tmux  # Debian/Ubuntu
sudo dnf install screen tmux  # Fedora/RHEL

# Start a screen session
screen

# Detach from screen session
Ctrl+A, then D

# List screen sessions
screen -ls

# Reattach to a screen session
screen -r session_id

Process Priority ​

nice and renice - Change Process Priority ​

bash
# Start a process with lower priority
nice -n 10 command  # Higher number means lower priority

# Change priority of running process
renice 10 -p PID

Nice values range from -20 (highest priority) to 19 (lowest priority).

Practice Exercises ​

  1. Start a process, send it to the background, and then bring it back to the foreground
  2. Find the processes using the most CPU and memory on your system
  3. Create a cron job that runs a script every day at midnight
  4. Use nohup to run a long-running command that will continue after you log out
  5. Start a service, check its status, and then stop it
  6. Change the priority of a running process
  7. Use screen or tmux to create a persistent terminal session

This completes our overview of Linux process management. With these skills, you can effectively monitor and control processes on your Linux system.