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.
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
- Create a Slack account (if you don't have it already) and (optionally) create a Slack workspace for testing.
- Go to api.slack.com/apps and create a new app.

- Open the application and, under the “Features” section, click on “Incoming Webhooks” and “Activate Incoming Webhooks”.

- 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.

- 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.

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.
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!

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.
Recommended Comments