How I Built a Server Health-Check & Slack Alerting Script Using Only Bash
In this tutorial, we will build a simple bash script that monitors server health and sends alerts to Slack via a webhook. The script checks four things: disk, memory, CPU, and a running service . If any of them crosses a threshold you set, the script sends a Slack message. Before you start You will need: A Working Laptop (this tutorial uses macOS & macOS commands) A terminal with bash A Slack…
In this tutorial, we will construct a straightforward Bash script designed to monitor server health and relay alerts to Slack through a webhook. The script will examine four aspects: disk space, memory usage, CPU performance, and the status of a specific service. Should any of these metrics surpass a predefined threshold, the script will promptly transmit a Slack notification.
Prior to commencing, ensure the following prerequisites are met: a functional laptop (this guide utilizes macOS with corresponding commands), access to a terminal equipped with Bash, a Slack account, and a note on macOS versus Linux compatibility—certain commands such as vm_stat and top -l 1 are exclusive to macOS, while df -h and pgrep are compatible across both platforms.
The tutorial will employ the vim text editor; should you prefer an alternative editor like VS Code, nano, or Cursor, you can directly utilize the provided code from the public repository.
The procedure unfolds in several stages, each accompanied by testing to ensure seamless functionality. Begin by creating a Slack webhook URL: navigate to api.slack.com/apps, select "Create New App," opt for the "Blank App" option, assign an identifier (e.g., "Server Health Bot"), and choose the workspace where alerts should be dispatched.
Within the sidebar, activate Incoming Webhooks, click "Add New Webhook to Workspace," select the desired channel, and subsequently click "Allow." Copy the generated webhook URL, which will resemble https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX. Exercise caution; treat this URL akin to a password, as unauthorized access could allow anyone to post to your channel.
The next step involves integrating the webhook URL into a configuration file named .env. In your terminal, execute the command: echo "SLACK_WEBHOOK_URL=https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX" > .env. This command utilizes output redirection to write the specified URL into the .env file, creating it if it doesn't exist or overwriting its contents if it does.
To safeguard this file from accidental inclusion in version control systems like Git, append .env to your .gitignore by running echo ".env" >> .gitignore. This ensures the file remains local and does not inadvertently share sensitive information.
With the foundational setup complete, we proceed to write the Bash script, beginning with the creation of a script file named health_check.sh. Open vim using the command vim health_check.sh, which places you in INSERT mode. Input the shebang at the top of the file: #!/bin/bash. This directive instructs the system on which interpreter should execute the script. Following this, press Esc to exit INSERT mode, and then type :wq to save your changes and exit vim.
Next, craft the reusable alerting function. This function will encapsulate the logic for posting messages to Slack, enabling each subsequent health check to report its findings directly to Slack. Retrieve your .env file (source /path/to/.env) and define the function as follows: send_alert (){ msg = $1 ; curl -X POST -H Content-type: application/json --data '{ "text" : "'"$msg"'" }' $SLACK_WEBHOOK_URL }.
Note the importance of using double quotes around the message variable to ensure proper variable expansion during the curl command.
With the alerting mechanism in place, we can now integrate individual health checks into our script. Begin by adding a disk check, a memory check, a CPU check, and finally, a service check. Each check will invoke the send_alert function if the monitored metric exceeds a specified threshold, ensuring prompt notification of any anomalies. This staged approach allows for systematic troubleshooting, enabling you to isolate and address any issues that arise during development.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.