#!/bin/bash # Set CPU usage we consider maximum, in % CPU utilisation maxCpu=100 # Setup our levels list and idle level (0% specifically) level=("012" "022" "021" "020" "120" "220" "210" "200") idleLevel="002" # Work out how many levels we have (level list length) levels=${#level[@]} # Check if we have any arguments if [ $# -eq 0 ]; then # No arguments provided, display usage echo "Usage: $0 procname" else # Arguments provided, run infinite loop observing process state while [ 1 ]; do # Read the process information stats=`ps -C "$1" -o %cpu | tail -n 1` # Get the list of processes matching a name, show only CPU usage and trim to only the last line # Check if we got any matches if [[ $stats = "%CPU" ]]; then # Note the double brackets '[[' and ']]' are needed for a literal match # Last line was the heading, this means no matches echo "000" > /dev/ledborg # Set LedBorg to off elif [[ $stats = " 0.0" ]]; then # Note the double brackets '[[' and ']]' are needed for a literal match # We had a match, however no CPU activity above 0.1% seen, label as idle echo "$idleLevel" > /dev/ledborg # Set colour to the idle value else # We had a match, pick a colour based on utilisation level cpuint=${stats/.*} # Truncate number to an integer util=$((($cpuint * $levels) / $maxCpu)) # Work out the slots as evenly spaced from 0% to maxCpu if [ $util -ge $levels ]; then # Check if we have a slot which is too high (at >= maxCpu this will happen) util=$(($levels - 1)) # Cap at highest slot fi echo ${level[$util]} > /dev/ledborg # Set colour based on utilisation fi # Wait for a period sleep 1 done fi