Specific process monitor indicator

This code takes the current load of a specific process and indicates this on the LedBorg by displaying colours to represent different process loads.

./procmon.sh name
where name is the process to monitor (use ps -A to list all current processes.

Colour displayed is the range from ~0% (<0.1%)(Blue) to 100% (Red). Black is used when no such process is running

<0.1% <13% <25% <38% <50% <63% <75% <88% >=88%

To run in the background call with an ampersand (&) at the end of the command, use killall to stop the program when finished, for example:
./procmon.sh find &
find /.
killall procmon.sh

For a better example find some source which uses gcc to build and try the following:
./procmon.sh cc1&
killall procmon.sh

  • To make script executable
    chmod +x procmon.sh
  • To copy to programs (use without ./ and .sh)
    sudo cp procmon.sh /usr/local/bin/procmon

Here's the code, you can download procmon script file as text here
Save the text file on your pi as procmon.sh


# Set CPU usage we consider maximum, in % CPU utilisation

# Setup our levels list and idle level (0% specifically)
level=("012" "022" "021" "020" "120" "220" "210" "200")

# Work out how many levels we have (level list length)

# Check if we have any arguments
if [ $# -eq 0 ]; then
    # No arguments provided, display usage
    echo "Usage: $0 procname"
    # 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
            # 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
            echo ${level[$util]} > /dev/ledborg         # Set colour based on utilisation

        # Wait for a period
        sleep 1