Ubuntu – Shell Script to Start Mysql Server if not running

MySQLshell-scriptingUbuntu

I have written a shell script to start mysql server & send a mail to admin user if it's restarted via shell script. What i am facing an issue if I run this shell script on terminal it's work perfectly & If same script runs via cronjob it's only sending the mail to the user & problem remains same. Is this problem relates to permission & how can i resolve it.

Shell Script——–

#!/bin/bash
EMAIL="abc@xyz.com"
SERVICE='mysql'
if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
   echo "$SERVICE service running, everything is fine"
else
   echo "$SERVICE is not running"
   /etc/init.d/mysql start
cat <<EOF | msmtp -a gmail $EMAIL
Subject: "Alert (Test Server) : Mysql Service is not running (Manually Restarted)"
Mysql Server Restarted at: `date`
EOF
EXIT

I am using msmtp for sending mail to the user on ubuntu 12.04 Server.

Best Answer

It seems to be an environnement issue, use complete path for binary like msmtp should resolve your problem.

Your script has same role than /usr/bin/mysqld_safe, maybe it's better for you to hack mysqld_safe to send you an email.

Related Topic