user2439278
user2439278

Reputation: 1214

How to send email on my shell script condition in jenkins

i had created a job and in build step i had given below mentioned shell script

# Shell script to monitor or watch the disk space
# It will send an email to $ADMIN, if the (free avilable) percentage 
# of space is >= 70% 
# -------------------------------------------------------------------------
# set admin email so that you can get email
# set alert level 70% is default

ALERT=70
EXCLUDE_LIST="/net|/home|devfs"
if [ "$EXCLUDE_LIST" != "" ] ; then
  df -H | grep -vE "^Filesystem|Users|${EXCLUDE_LIST}" | awk '{ print $5 " " $1 }' | while read output;
do
  #echo $output
  usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1  )
  partition=$(echo $output | awk '{ print $2 }' )
  if [ $usep -ge $ALERT ]; then
    echo "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)" > /Users/Shared/Jenkins/disk_space.txt
else
    echo "space \"$partition ($usep%)\" on $(hostname) as on $(date)" > /Users/Shared/Jenkins/space.txt
  fi
done
fi

After this script executed the email need to be triggered in jenkins if the condition satisfied. otherwise job should run but email should not trigger.

Upvotes: 1

Views: 9640

Answers (2)

Peter Schuetze
Peter Schuetze

Reputation: 16305

Looks like a standard question on how to send an email from a shell script: Have a look at the following links.

Shell script to send email

http://theos.in/shell-scripting/send-mail-bash-script/

http://www.cyberciti.biz/faq/linux-unix-bash-ksh-csh-sendingfiles-mail-attachments/

Upvotes: 1

San7988
San7988

Reputation: 106

One way may be to call another jenkins job within the if loop, using jenkins cli.
You can have another job configured which will trigger a mail with certain content. And that job will be triggered within the success part of your if loop

More info on jenkins CLI can be found at here
May be this will be helpful.

Upvotes: 0

Related Questions