This project provides a script for monitoring the health of various web services and automatically restarting them if they become unresponsive. It's designed to be easy to configure and deploy on any system running systemd.
- Health Checks: Regularly checks the health of configured web services.
- Automatic Service Restart: Automatically restarts services if their health check fails.
- Log Rotation: Implements a basic log rotation mechanism to prevent log files from growing indefinitely.
- Configurable: All services are defined in a separate JSON configuration file for easy management.
- Error Handling: Robust error handling to manage different failure scenarios.
- Service Restart Policy: Limits the number of restart attempts and adds delay between attempts.
- A Unix-like operating system with systemd
curl
andjq
installed on your system- Sudo privileges (for restarting services and creating log files)
-
Clone the repository:
git clone [your-repo-url]
-
Navigate to the script directory:
cd [your-directory]
-
Make the script executable:
chmod +x check_apis.sh
-
Update the
services_config.json
with your service names and health check URLs.
Run the script manually: ./check_apis.sh
Or set it up as a cron job to run automatically at your desired frequency.
Edit your crontab: crontab -e
Add a line like the following to run the script every 2 minutes: */2 * * * * /path/to/check_apis.sh
The services_config.json` file should be structured as follows: { "service_name1": "health-check-url-1", "service_name2": "health-check-url-2" }
Replace service1, service2, etc., with your service names, and health-check-url-1, health-check-url-2 with the corresponding health check URLs.
Logs are stored in log directory. Log files are rotated based on size, as defined in the script.
Contributions are welcome! Feel free to open issues or submit pull requests.