|
| 1 | +#!/bin/bash |
| 2 | +# This script list the top 5 CPU, memory, input/output (I/O), and network consuming processes |
| 3 | +# This script should work on both CentOS and macOS |
| 4 | +# Author: Prashant Lakhera(laprashant@gmail.com) |
| 5 | + |
| 6 | +# Check if top command is present |
| 7 | +if command -v top >/dev/null 2>&1; then |
| 8 | + echo "top command is present" |
| 9 | +else |
| 10 | + echo "top command is not present. Installing..." |
| 11 | + # Install top command |
| 12 | + if [ -f /etc/centos-release ]; then |
| 13 | + # CentOS |
| 14 | + sudo yum install -y procps-ng |
| 15 | + elif [ -f /etc/lsb-release ]; then |
| 16 | + # Ubuntu |
| 17 | + sudo apt-get update |
| 18 | + sudo apt-get install -y top |
| 19 | + else |
| 20 | + # Unsupported OS |
| 21 | + echo "Unsupported operating system" |
| 22 | + exit 1 |
| 23 | + fi |
| 24 | +fi |
| 25 | + |
| 26 | +# Function to list top 5 CPU consuming process |
| 27 | +list_top_cpu_consuming_processes() { |
| 28 | + # Get the top 5 processes by CPU usage |
| 29 | + top_five_cpu_processes=$(ps -eo pcpu,pid,user,args | sort -k 1 -r | head -n 5) |
| 30 | + |
| 31 | + # Print the results |
| 32 | + echo "###############################################################################" |
| 33 | + echo "Top 5 CPU consuming processes:" |
| 34 | + echo "$top_five_cpu_processes" |
| 35 | +} |
| 36 | + |
| 37 | +# Function to list the top 5 processes by memory usage |
| 38 | +list_top_memory_consuming_processes() { |
| 39 | + # Get the top 5 processes by memory usage |
| 40 | + top_five_memory_processes=$(ps -eo pmem,pid,user,args | sort -k 1 -r | head -n 5) |
| 41 | + |
| 42 | + # Print the results |
| 43 | + echo "###############################################################################" |
| 44 | + echo "Top 5 memory consuming processes:" |
| 45 | + echo "$top_five_memory_processes" |
| 46 | +} |
| 47 | + |
| 48 | +# Check if iotop command is present |
| 49 | +if command -v iotop >/dev/null 2>&1; then |
| 50 | + echo "iotop command is present" |
| 51 | +else |
| 52 | + echo "iotop command is not present. Installing..." |
| 53 | + # Install top command |
| 54 | + if [ -f /etc/centos-release ]; then |
| 55 | + # CentOS |
| 56 | + sudo yum install -y epel-release |
| 57 | + sudo yum install -y iotop |
| 58 | + elif [ -f /etc/lsb-release ]; then |
| 59 | + # Ubuntu |
| 60 | + sudo apt-get update |
| 61 | + sudo apt-get install -y iotop |
| 62 | + else |
| 63 | + # Unsupported OS |
| 64 | + echo "Unsupported operating system" |
| 65 | + exit 1 |
| 66 | + fi |
| 67 | +fi |
| 68 | + |
| 69 | +# Function to list the top 5 processes by I/O usage |
| 70 | +list_top_io_consuming_processes() { |
| 71 | + # Get the top 5 processes by I/O usage |
| 72 | + top_five_io_processes=$(sudo iotop -o -b -n 5) |
| 73 | + |
| 74 | + # Print the results |
| 75 | + echo "###############################################################################" |
| 76 | + echo "Top 5 I/O consuming processes:" |
| 77 | + echo "$top_five_io_processes" |
| 78 | +} |
| 79 | + |
| 80 | +# Check if iftop command is present |
| 81 | +if command -v iftop >/dev/null 2>&1; then |
| 82 | + echo "iftop command is present" |
| 83 | +else |
| 84 | + echo "iftop command is not present. Installing..." |
| 85 | + # Install top command |
| 86 | + if [ -f /etc/centos-release ]; then |
| 87 | + # CentOS |
| 88 | + sudo yum install -y iftop |
| 89 | + elif [ -f /etc/lsb-release ]; then |
| 90 | + # Ubuntu |
| 91 | + sudo apt-get update |
| 92 | + sudo apt-get install -y iftop |
| 93 | + else |
| 94 | + # Unsupported OS |
| 95 | + echo "Unsupported operating system" |
| 96 | + exit 1 |
| 97 | + fi |
| 98 | +fi |
| 99 | + |
| 100 | + |
| 101 | +# Function to list the top 5 processes by network usage |
| 102 | +list_top_network_consuming_processes() { |
| 103 | + # Get the top 5 processes by network usage |
| 104 | + top_five_network_processes=$(sudo iftop -P -n -t -s 5) |
| 105 | + |
| 106 | + # Print the results |
| 107 | + echo "###############################################################################" |
| 108 | + echo "Top 5 network consuming processes:" |
| 109 | + echo "$top_five_network_processes" |
| 110 | +} |
| 111 | + |
| 112 | + |
| 113 | +# Main function |
| 114 | +main() { |
| 115 | + list_top_cpu_consuming_processes |
| 116 | + list_top_memory_consuming_processes |
| 117 | + list_top_io_consuming_processes |
| 118 | + list_top_network_consuming_processes |
| 119 | +} |
| 120 | + |
| 121 | +# Run the main function |
| 122 | +main |
0 commit comments