The Georgia The Georgia - 2 months ago 6
Bash Question

Shell script to check if mysql is up or down

I want a bash shell script that i can run using a cron job to check if mysql on a remote server is running. If it is, then do nothing, other start the server.

The cronjob will be checking the remote server for a live (or not) mysql every minute. I can write the cron job myself, but i need help with the shell script that checks if a remote mysql is up or down. The response after a check if up or down is not important. But the check is important.

Answer

You can use below script

#!/bin/bash

USER=root
PASS=root123

mysqladmin -h remote_server_ip -u$USER -p$PASS processlist ###user should have mysql permission on remote server. Ideally you should use different user than root.

 if [ $? -eq 0 ]
        then
                echo "do nothing"
        else
                ssh remote_server_ip  ###remote server linux root server password should be shared with this server.
                service mysqld start
 fi