Category Archives: Grep

Email alert for low disk space v2

A while back I wrote a post about a simple low disk space script that will email you if a specific partition on your system has free space lower than a percentage you specified. Now I improved that script, adding some more features:

– excluding some partitions (be careful here not too exclude to much)

– sending email to many recipients

– check if emails were already sent, then it would spam the users with emails

The script is below:

DF=( `df -h | grep % | tail -n +2 | awk '{print $(NF-2)" "$(NF-1)" "$(NF)}' | sed 's/%//g' | sed 's#/\(.*\)#/\1 slash-\1#g' ` ) 
for ((i=1;i<${#DF[@]};i=i+4)) 
  if [[ ${DF[i]} -gt $WARNING ]] 
    if [ ! -f /tmp/disk-warning-for-${DF[i+2]} ] 
      touch /tmp/disk-warning-for-${DF[i+2]} 
      echo "" | mail -s "SH Alert: Critical: `hostname` - ${DF[i+1]} has only ${DF[i]}% (${DF[i-1]}) free space" $RECIPIENTS 
    if [ -f /tmp/disk-warning-for-${DF[i+2]} ] 
      echo "" | mail -s "SH Alert: Clear: `hostname` - ${DF[i+1]} has only ${DF[i]}% (${DF[i-1]}) free space" $RECIPIENTS 
      rm /tmp/disk-warning-for-${DF[i+2]} 

As in the previous post, you need to make the script executable:

chmod +x /path/to/script

And add it to the cronjob using “cronjob -e”:

# Minute   Hour   Day of Month       Month          Day of Week        Command
# (0-59)  (0-23)     (1-31)    (1-12 or Jan-Dec)  (0-6 or Sun-Sat)
*/10 * * * * /path/to/script

Note: This was tested on Oracle Linux 5/6 and is working great. For other distro’s you might need to change the email syntax, the order of the df columns, etc.

Search Multiple Words with grep

If you are looking for searching at least 2 words in a file with grep, you can use the following:

grep 'word1\|word2\|word3' path_to_file

grep -E 'word1|word2|word3' path_to_file

and if you want to show the the lines where all the words are on the same line use -w like so:

grep -w 'word1\|word2\|word3' path_to_file


Note: I’ve seen -w missing some of the output when you only put one word (usually in scripts). So i only used -E switch and it seemed to work. It showed all the searched word on the same line.

Enjoy scripting! 🙂