eekfonky
eekfonky

Reputation: 855

Stop stdout to terminal in bash

I have a wee script to check on the MySQL replication status. However it prints out the API that is used to send to another server for monitoring. Here is the script;

#!/bin/bash

# set up monitoring
TEST_TYPE="MySQL Replication Status"
start_date=$(date --iso-8601=seconds)
SERVER_NAME=$(hostname -f)

# Checks MySQL Replication status. Sends user(s) a notification of status
status=0
SlaveHost="localhost"
emails="[email protected]" #multiple emails space separated
DownSubject="Replication status - Down $(hostname -f)"
GoodMessage="MySQL replication on $SlaveHost is good"

#Grab the lines for each and use awk to get the last part of the string(Yes/No)
SQLresponse=$(mysql --login-path=dbbkup hydrosense_live -e "show slave status\G" | grep -i "Slave_SQL_Running" | awk '{print $2}')
IOresponse=$(mysql --login-path=dbbkup hydrosense_live -e "show slave status\G" | grep -i "Slave_IO_Running" | awk '{print $2}')
if [ "$SQLresponse" = "No" ]; then
error="Replication on the slave MySQL server($SlaveHost) has stopped working.nSlave_SQL_Running: Non"
status=1
fi
if [ "$IOresponse" = "No" ]; then
error="Replication on the slave MySQL server($SlaveHost) has stopped working.nSlave_IO_Running: Non"
status=1
fi

# Replication status
if [ $status = 1 ]; then
for address in $emails; do
echo -e $error | mail -s "$DownSubject" $address
echo "Replication down, sent email to $address"
done
RESULT=0
else echo -e $GoodMessage
RESULT=1
fi

curl --request POST \
  --url https://ops.server.com/api/status/testruns/ \
  --user 'test:test!' \
  --header 'content-type: application/json' \
  --data "{\"server\": \"$SERVER_NAME\", \"test\": \"$TEST_TYPE\", \"start_date\": \"$start_date\", \"end_date\": \"$(date --iso-8601=seconds)\", \"result\": $RESULT}"

When it runs it prints out the following;

MySQL replication on localhost is good
{"start_date":"2016-12-19T12:45:16Z","end_date":"2016-12-19T12:45:16Z","server":"web-3.hydrosensepro.com","test":"MySQL Replication Status","result":1,"message":""}%

The first line is OK but I don't want the rest. How can I remove it from the stdout?

Upvotes: 0

Views: 915

Answers (2)

tripleee
tripleee

Reputation: 189948

Your script is a mess. Probably the whole thing should be refactored to print human-readable status messages to standard error, and machine-readable output on standard output. Then you can selectively disable one or the other with

./script >/dev/null  # to see only the human-readable status messages

or

./script 2>/dev/null  # for the machine-readable output

The way to do this is to change

echo "message"

to

echo "message" >&2

throughout.

(I can think of a number of other fixes but this should get you started.)

Upvotes: 0

Mattasse
Mattasse

Reputation: 1171

I think the best way is to disabled output of your command with this at the end :

>&- 2>&- 

Or check manual if option exist to run it on silent mode. And after you can catch the retour code to personalize the output :

if [ $? -eq 0 ]; then
    echo MySQL replication on localhost is good
else 
    echo MySQL replication on localhost is not good
fi

Upvotes: 1

Related Questions