A simple bash script to monitor the MariaDB service. The script checks if MariaDB is running, and if it detects that the service has gone down, it attempts to restart it automatically. Logs of these actions are maintained for easy debugging and monitoring.
- Monitors MariaDB service status.
- Automatically restarts MariaDB if it's not running.
- Logs all actions to
/var/log/mariadb_monitor.log
. - Configurable check interval.
- Bash (v4 or higher)
- System with
systemctl
for service management (e.g., most Linux distributions with systemd). - MariaDB installed and running as a service.
-
Clone this repository:
git clone https://github.com/<your-username>/mariadb-monitor.git cd mariadb-monitor
-
Make the script executable:
chmod +x mariadb_monitor.sh
-
(Optional) Move the script to a directory in your
PATH
for easier usage:sudo mv mariadb_monitor.sh /usr/local/bin/mariadb_monitor
Run the script to start monitoring MariaDB:
./mariadb_monitor.sh
Or, if you moved it to /usr/local/bin
:
mariadb_monitor
To keep the script running even after closing the terminal, use:
nohup ./mariadb_monitor.sh &
Alternatively, you can use a tool like screen
or set it up as a systemd service (see below).
All actions are logged to /var/log/mariadb_monitor.log
. Make sure the user running the script has permission to write to this file.
To run the monitor script as a systemd service:
-
Create a new service file:
sudo nano /etc/systemd/system/mariadb-monitor.service
-
Add the following content:
[Unit] Description=MariaDB Monitor Script After=network.target mariadb.service [Service] ExecStart=/path/to/mariadb_monitor.sh Restart=always [Install] WantedBy=multi-user.target
-
Reload systemd and enable the service:
sudo systemctl daemon-reload sudo systemctl enable mariadb-monitor sudo systemctl start mariadb-monitor
Contributions are welcome! Feel free to open issues or submit pull requests.
This project is licensed under the MIT License. See the LICENSE file for details.