Jump to content

Welcome to CodeNameJessica

Welcome to CodeNameJessica!

💻 Where tech meets community.

Hello, Guest! 👋
You're just a few clicks away from joining an exclusive space for tech enthusiasts, problem-solvers, and lifelong learners like you.

🔐 Why Join?
By becoming a member of CodeNameJessica, you’ll get access to:
In-depth discussions on Linux, Security, Server Administration, Programming, and more
Exclusive resources, tools, and scripts for IT professionals
A supportive community of like-minded individuals to share ideas, solve problems, and learn together
Project showcases, guides, and tutorials from our members
Personalized profiles and direct messaging to collaborate with other techies

🌐 Sign Up Now and Unlock Full Access!
As a guest, you're seeing just a glimpse of what we offer. Don't miss out on the complete experience! Create a free account today and start exploring everything CodeNameJessica has to offer.

by: LHB Community
Mon, 12 May 2025 10:43:21 +0530


Automating tasks is great, but what's even better is knowing when they're done or if they've gotten derailed.

Slack is a popular messaging tool used by many techies. And it supports bots that you can configure to get automatic alerts about things you care about.

Web server is down? Get an alert. Shell script completes running? Get an alert.

Yes, that could be done too. By adding Slack notifications to your shell scripts, you can share script outcomes with your team effortlessly and respond quickly to issues and stay in the loop without manual checks. It lets you monitor automated tasks without constantly checking logs. 

🚧
I am assuming you already use Slack and you have a fair idea about what a Slack Bot is. Of course, you should have at least basic knowledge of Bash scripting.

The Secret Sauce: curl and Webhooks

The magic behind delivering Slack notifications from shell scripts is Slack's Incoming Webhooks and the curl command line tool.

Basically, everything is already there for you to use, it just needs some setup for connections. I found it pretty easy, and I'm sure you will too.

Here are the details for what webhooks and the command line tool is for:  

  • Incoming Webhooks: Slack allows you to create unique Webhook URLs for your workspace that serve as endpoints for sending HTTP POST requests containing messages.  
  • curl: This powerful command-line tool is great for making HTTP requests. We'll use it to send message-containing JSON payloads to Slack webhook URLs.

Enabling webhooks on Slack side

  1. Create a Slack account (if you don't have it already) and (optionally) create a Slack workspace for testing.
  2. Go to api.slack.com/apps and create a new app.
create a new slack app
  1. Open the application and, under the “Features” section, click on “Incoming Webhooks” and “Activate Incoming Webhooks”.
slack webhook activate
  1. Under the same section, scroll to the bottom. You’ll find a button “Add New Webhook to Workspace”. Click on it and add the channel.
webhook connection to workspace
  1. Test the sample CURL request. 

Important: The CURL command you see above also has the webhook URL. Notice that https://hooks.slack.com/services/xxxxxxxxxxxxx things? Note it down.

Sending Slack notifications from shell scripts

Set SLACK_WEBHOOK_URL environment variable in your .bashrc file as shown below.

webhook url
Use the webhook URL you got from Slack in the previous step

Create a new file, notify_slack.sh, under your preferred directory location.

# Usage: notify_slack "text message"
# Requires: SLACK_WEBHOOK_URL environment variable to be set
notify_slack() {
    local text="$1"
    curl -s -X POST -H 'Content-type: application/json' \
        --data "{\"text\": \"$text\"}" \
        "$SLACK_WEBHOOK_URL"
}

Now, you can simply source this bash script wherever you need to notify Slack. I created a simple script to check disk usage and CPU load.

source ~/Documents/notify_slack.sh 
disk_usage=$(df -h / | awk 'NR==2 {print $5}')
# Get CPU load average
cpu_load=$(uptime | awk -F'load average:' '{ print $2 }' | cut -d',' -f1 | xargs)
hostname=$(hostname)
message="*System Status Report - $hostname*\n* Disk Usage (/): $disk_usage\n* CPU Load (1 min): $cpu_load"
# Send the notification
notify_slack "$message"

Running this script will post a new message on the Slack channel associated with the webhook.

Slack notifications from bash shell

Best Practices 

It is crucial to think about security and limitations when you are integrating things, no matter how insignificant you think it is. So, to avoid common pitfalls, I recommend you to follow these two tips:

  • Avoid direct hardware encoding in publicly shared scripts. Consider using environment variables or configuration files.
  • Be aware of Slack's rate limitation for incoming webhooks, especially if your scripts may trigger notifications frequently. You may want to send notifications only in certain circumstances (for example, only on failure or only for critical scripts).

Conclusion

What I shared here was just a simple example. You can utilize cron in the mix and periodically send notifications about server stats to Slack. You put in some logic to get notified when disk usage reaches a certain stage.

There can be many more use cases and it is really up to you how you go about using it. With the power of Incoming Webhooks and curl, you can easily deliver valuable information directly to your team's communication center. Happy scripting!

CTA Image

Bhuwan Mishra is a Fullstack developer, with Python and Go as his tools of choice. He takes pride in building and securing web applications, APIs, and CI/CD pipelines, as well as tuning servers for optimal performance. He also has a passion for working with Kubernetes.

0 Comments

Recommended Comments

There are no comments to display.

Guest
Add a comment...

Important Information

Terms of Use Privacy Policy Guidelines We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.