FAUN — Developer Community 🐾

We help developers learn and grow by keeping them up with what matters. 👉 www.faun.dev

Follow publication

Let’s go $HOME ❤ Photo by Jehu Christan on Unsplash

Easy notifications from Bash scripts!

Alberto Marchetti
FAUN — Developer Community 🐾
3 min readNov 18, 2020

--

I’ve had this thought popping up endless times in the past:

Oh, I wish there was a easy way to track Bash scripts execution and that I could know whenever they finish, or whenever there are errors, without having to manually check it all the time!

Luckily, there are ways to solve this! One of them, which I like the most, is a one-liner:

n17() { sh -c "$(wget -qO - "https://n17.io/sh")" -- -k "N17_RAW_API_KEY" "$@"; }

And you use it this way:

mycommand; n17 -t "Script execution finished!"

Which will produce:

Note: you can see some more examples in Notify17’s docs.

Wow, how do I get this?

To set up this script, all you need to do is:

  • Create a Notify17 account, it’s free!
  • Next, create a raw API key from the dashboard and copy it. Remember to label the key meaningfully, e.g. Bash scripts, so you don't forget what it's used for.
  • Replace the N17_RAW_API_KEY text in the Bash function with your raw API key.
  • Use the script!
  • Optional but greatly suggested — download the app for Android or iOS to receive notifications on mobile devices.

Some examples

This is how you can use the n17 function in different contexts:

# Simple title-only notification  
n17 -t "Hello!"

# Notification with multiline content
n17 -t "Hello!" -c "This is a \nmultiline string"

# Process stdin
echo "I come from another command!" | n17 -t "Result" -c -

# Process stdin (title)
echo "I come from another command!" | n17 -t -

# Notification on success/error
command && n17 -t "Success!"
command || n17 -t "Failed!"

Bash errors trap

And what if we wanted to catch Bash errors?

set -e

# Define the n17 function
n17() { sh -c "$(wget -qO - "https://n17.io/sh")" -- -k "N17_RAW_API_KEY" "$@"; }

# This function will be invoked on script error.
n17TrapErr() {
n17 \
-t "Script execution error!" \
-c "$(cat <<CONTENT
Script ${BASH_SOURCE[0]} has encountered an error:

Exit code: $1
Line: $2
Command: $3

Trap arguments: $@
CONTENT
)"
}

# Set up the error trap, which will forward all desired info for each error
trap 'n17TrapErr "$?" "$LINENO" "$BASH_COMMAND"' ERR

## This function will always throw an error
## (127, command not found)
thisCommandDoesNotExist

And we’ll get:

Enjoy! 😄

👋 Join FAUN today and receive similar stories each week in your inbox! Get your weekly dose of the must-read tech stories, news, and tutorials.

Follow us on Twitter 🐦 and Facebook 👥 and Instagram 📷 and join our Facebook and Linkedin Groups 💬

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author! ⬇

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response