Service Health Check Script
This Bash script checks the health of specified services and sends notifications to Slack if any service is not responding with a 200 OK status.
Note: This is as basic as it gets
Features
- Checks the health of multiple services
- Sends Slack notifications when a service goes down or comes back up
- Runs checks in parallel for efficiency
- Maintains service status in a JSON file to track changes
How it works
- The script iterates through the list of services.
- For each service, it sends a GET request and checks the HTTP status code.
- If a service is down (non-200 response) and was previously up, it sends a Slack notification.
- If a service is up (200 response) and was previously down, it sends a Slack notification.
- The current status of each service is stored in a JSON file (
service_status.json).
Prerequisites
- Bash shell
curlcommand-line tool- Slack webhook URL
jqcommand-line JSON processor
Configuration
- Edit the
servicesarray to include the URLs of the services you want to monitor. - Set the
slack_webhookvariable with your Slack webhook URL.
Usage
-
Make the script executable:
-
Run the script:
It's recommended to set up a cron job to run this script at regular intervals.
Running with Cron
To run this script every minute using cron:
-
Open the crontab file:
-
Add the following line to run the script every minute:
* * * * * /path/to/uptime.shReplace
/path/to/with the actual path to your script. -
Save and exit the crontab editor.
The script will now run automatically every minute.
Customization
- Add more services to the
servicesarray as needed. - Modify the
check_servicefunction to adjust the health check criteria or add more complex checks.
Note
Please ensure the Slack webhook URL is kept confidential and not shared publicly.