Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7569199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:06:38+00:00 2026-05-30T15:06:38+00:00

I am trying to write a simple Bash script to monitor MySQL replication status.

  • 0

I am trying to write a simple Bash script to monitor MySQL replication status. The script is like this:

#!/bin/bash
dbhost=192.168.1.2
repluser=root
replpasswd=password
echo "show slave status\G"|\
mysql -h $dbhost -u $repluser -p$replpasswd > tmpd 2>/dev/null
repl_IO=$(cat tmpd | grep "Slave_IO_Running" | cut -f2 -d':')
repl_SQL=$(cat tmpd | grep "Slave_SQL_Running" | cut -f2 -d':')
if [ $repl_IO != "Yes" -o $repl_SQL != "Yes" ] ; then
     echo
     echo -e "\033[31m Replication Error."
     echo -e "\033[0m"
     mail -s "replication error" email@domain.com < tmpd

else
     echo
     echo -e "\033[32mReplication is working fine"
     echo -e "\033[0m"

fi

The problem is that the script only works if both the master and the slave are up. If the master is down, and I run the script, it displays the error message and sends the email.
If both master/slave are up, the script displays “Replication is working fine” which is okay. But when I shutdown the slave and run the script, I get this error:

./monitor.bash: line 9: [: too many arguments

Replication is working fine

I know the problem is that since I’m querying the slave MySQL server, it’s not finding it. So it’s not checking the conditions Slave_IO_Running and Slave_SQL_Running. How would I go
about checking if the slave server is up BEFORE running those conditions. So in short, I only want “Replication is working fine” to be displayed, if both the master & slave are up and
running and it matches those conditions. Any help would be appreciated. Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:06:39+00:00Added an answer on May 30, 2026 at 3:06 pm

    If $repl_IO and $repl_SQL are blank, then this:

    if [ $repl_IO != "Yes" -o $repl_SQL != "Yes" ] ; then
    

    is equivalent to this:

    if [ != Yes -o != Yes ] ; then
    

    and I think you can see why that doesn’t work. You need either to wrap your parameter-expansions in double-quotes, so that they’re treated as single arguments no matter what they contain:

    if [ "$repl_IO" != "Yes" -o "$repl_SQL" != "Yes" ] ; then
    

    or to use [[...]] instead of [...], since it’s a bit smarter with these things:

    if [[ $repl_IO != "Yes" -o $repl_SQL != "Yes" ]] ; then
    

    or both:

    if [[ "$repl_IO" != "Yes" -o "$repl_SQL" != "Yes" ]] ; then
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a simple bash script that will search for files
I'm trying to write a simple bash script that accepts all arguments and interprets
I'm trying to write (what I thought would be) a simple bash script that
I'm trying to write a simple bash script which should copy all files with
I am trying to write a simple bash script. It just puts out a
I am trying to write a simple bash script that is listening on a
I am trying to write what should be a super simple bash script. Basically
I am trying to write a simple bash script that will copy the entire
I'm trying to write a simple bash script that takes two parameters and sums
I'm trying to write a simple bash script but something seems wrong, I'm testing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.