Search code examples
bashrhel

Bash Script that gets a count of running processes and then emails if over a certain threshold


I am trying to create a script to grab the current number of running processes and the if that number is over 1000 then send me an email. I am trying to do this in a bash script that I will just use a cron job to call it. The code I am using is below and I am sure I just have something out of place and just need another set of eyes.

PCOUNT=$(cat /proc/loadavg|awk '{print $4}'|awk -F/ '{print $2}')
if [$PCOUNT > 100]; then
    mail -s "Process Count" email@example.com
fi

Solution

  • After alot of trial and error I finally found a solution. I ended up taking the output of the awk statement and writing it to a file. I then cat the file and send the output to mail.

    PCOUNT=$(awk -F" |/" '{print $5}' /proc/loadavg)
    if ((PCOUNT>1000)); then
        echo "Number of Running Processes is" $PCOUNT >>/tmp/mail.txt
        cat /tmp/mail.txt | mail -s "Number of processes is rising" example@email.com
    fi