🐧 Week 2: Linux + Networking Basics

Day 4: Process & Package Management

Duration: 5 Hours

📚 Learning Objectives

By the end of this session, you will be able to:

  • View and manage running processes
  • Start, stop, and monitor services
  • Use package managers to install software
  • Monitor system resources
  • Understand systemd and service management

📖 Core Concepts (2 Hours)

Process Management

# View running processes ps # Current shell processes ps aux # All processes (detailed) ps aux | grep nginx # Find specific process # Real-time process viewer top # Interactive process viewer htop # Better version (install: apt install htop) # Process details ps aux columns: # USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND # Kill processes kill PID # Graceful termination (SIGTERM) kill -9 PID # Force kill (SIGKILL) killall processname # Kill by name pkill pattern # Kill by pattern # Background processes command & # Run in background jobs # List background jobs fg %1 # Bring job 1 to foreground bg %1 # Send job 1 to background nohup command & # Run even after logout

System Resources

# Memory usage free -h # Human readable free -m # In megabytes # Disk usage df -h # Disk space du -sh /path # Directory size du -sh * | sort -h # Sort directories by size # CPU info lscpu # CPU information cat /proc/cpuinfo # Detailed CPU info # System uptime uptime # 10:30:45 up 15 days, 3:45, 2 users, load average: 0.52, 0.58, 0.59 # Load average: 1min, 5min, 15min # Rule: Load should be < number of CPU cores

Service Management (systemd)

# Service commands sudo systemctl status nginx # Check service status sudo systemctl start nginx # Start service sudo systemctl stop nginx # Stop service sudo systemctl restart nginx # Restart service sudo systemctl reload nginx # Reload config without restart # Enable/disable on boot sudo systemctl enable nginx # Start on boot sudo systemctl disable nginx # Don't start on boot # List services systemctl list-units --type=service systemctl list-units --type=service --state=running # View service logs journalctl -u nginx # Logs for nginx journalctl -u nginx -f # Follow logs journalctl -u nginx --since "1 hour ago"

Package Management - Ubuntu/Debian (apt)

# Update package list sudo apt update # Upgrade all packages sudo apt upgrade # Install package sudo apt install nginx sudo apt install -y nginx # Auto-yes # Remove package sudo apt remove nginx # Remove but keep config sudo apt purge nginx # Remove everything sudo apt autoremove # Remove unused dependencies # Search for package apt search nginx apt show nginx # Package details # List installed packages apt list --installed apt list --installed | grep nginx

Package Management - CentOS/RHEL (yum/dnf)

# DNF (newer) or YUM (older) sudo dnf update # Update all sudo dnf install nginx # Install sudo dnf remove nginx # Remove sudo dnf search nginx # Search dnf list installed # List installed # For older systems, replace dnf with yum

🔬 Hands-on Lab (2.5 Hours)

Lab 1: Process Exploration

  • View all running processes
  • Find specific processes
  • Monitor with top/htop
# Lab 1: Process commands # View all processes ps aux # Find Python processes ps aux | grep python # Start a background process sleep 100 & # View background jobs jobs # Kill it kill %1 # Start a process that keeps running ping localhost > /dev/null & # Find and kill it ps aux | grep ping kill $(pgrep ping)

Lab 2: Package Management

  • Update system packages
  • Install useful tools
  • Remove packages cleanly
# Lab 2: Install useful DevOps tools # First update sudo apt update # Install useful tools sudo apt install -y \ htop \ tree \ curl \ wget \ jq \ net-tools # Verify installations htop --version tree --version curl --version jq --version # View directory tree tree ~/devops-project # Use jq to parse JSON echo '{"name":"test","value":123}' | jq '.name'

Lab 3: Service Management

  • Install and configure a web server
  • Start, stop, restart the service
  • View service logs
# Lab 3: Nginx web server # Install nginx sudo apt install -y nginx # Check status sudo systemctl status nginx # Start if not running sudo systemctl start nginx # Enable on boot sudo systemctl enable nginx # Test - should see nginx welcome page curl http://localhost # View logs sudo journalctl -u nginx --no-pager | tail -20 # Stop and disable (cleanup) sudo systemctl stop nginx sudo systemctl disable nginx

Lab 4: Resource Monitoring

  • Check memory usage
  • Monitor CPU load
  • Find disk space issues
# Create monitoring script cat > ~/devops-project/scripts/system-health.sh << 'EOF' #!/bin/bash echo "====== System Health Report ======" echo "" echo "=== Uptime ===" uptime echo "" echo "=== Memory Usage ===" free -h echo "" echo "=== Disk Usage ===" df -h | grep -v tmpfs echo "" echo "=== Top 5 Processes by CPU ===" ps aux --sort=-%cpu | head -6 echo "" echo "=== Top 5 Processes by Memory ===" ps aux --sort=-%mem | head -6 echo "==================================" EOF chmod +x ~/devops-project/scripts/system-health.sh ~/devops-project/scripts/system-health.sh

📝 Practice Exercises

  1. Find all processes owned by your user
  2. Install tree and use it to visualize a directory structure
  3. Create a script that logs memory usage every 5 seconds
  4. Find the top 10 largest files in /var/log

💡 DevOps Relevance

  • Process monitoring: Identify runaway processes, memory leaks
  • Service management: Deploy and manage applications
  • Package management: Automate software installation
  • Resource monitoring: Capacity planning, troubleshooting

✅ Day 4 Checklist

  • Can view and filter processes (ps, top, htop)
  • Can kill processes by PID or name
  • Can manage services with systemctl
  • Can install/remove packages with apt
  • Can monitor system resources
  • Installed useful DevOps tools