Mutable Ideas

Notes and ideas about Java, Scala, Big Data, NoSQL, Quality and Software Deploy

Be Notified on Slack When a Long Process Finishes

We run several processes that may take hours to complete and it is nice to be notified on a Slack channel when those processes finishes correctly. Using the Slack’s Incoming Webhooks API, a small bash script and a couple of tricks it is really simple!

slack-bash

Setup

Only few steps are necessary to install this small script:

  1. Download this bash script and place it at your home directory:
1
curl https://gist.githubusercontent.com/arjones/9750b39a26c63b9d3775b5334cadf11b/raw/1f0c1d2e5458f0727c0f8cc3c8b18f638f76bc7f/notify-slack.sh > ~/notify-slack
  1. Edit your ~/.bashrc to source the new file:
1
2
# Add function to send bash notifications to a Slack Channel
[ -f ~/notify-slack ] && source ~/notify-slack
  1. Create a file at ~/.notify-slack-cfg containing the Slack’s webhook. This file MUST contains only the key, it looks like T02TKKSAX/B246MJ6HX/WXt2BWPfNhSKxdoFNFblczW9. You can create your webhook here

  2. All done! :D

Usage

Now you can test notifications, just source your .bashrc again: source ~/.bashrc and let’s do a few tests:

1
notify-slack 'Hello World!'

You should receive inmediatlely a notification on Slack:

slack-hello-world

If you start your message with INFO, WARN or ERROR a nice icon will be prepended.

1
notify-slack 'ERROR: Something went wrong :('

slack-error-message

Chaining on your scripts

Chain notify-slack on your own commands and scripts. I use it to be notify when a backup finished correctly:

1
(mysqldump test | gzip -9 > test.gz) && notify-slack 'INFO: Backup has finished'

slack-error-message

Hope you find it useful! Enjoy!

Comments