3

i wrote a script that make a dump of mysql dbs, i need to check if the command succfully executed or not. using $? give me always 0 even if there are a connection problem with the db server. any idea how can i do this thanks

2
  • Please show us your code, as it seems that mysql does provide a valid exit status... stackoverflow.com/questions/21369396/… Commented May 9, 2014 at 10:30
  • mysqldump -u usr -ppwd db | gzip -c > db.sql.g echo $? # test exit status Commented May 9, 2014 at 11:24

1 Answer 1

4

Script

#!/bin/sh
mysqldump -h 192.168.1.10 -u user -pPaSSwOrD dbname > filename.sql
if [ "$?" -eq 0 ]; then
    echo "Success"
else
    echo "Error"
fi

is successfully showing errors for connection timeouts, wrong dbname, wrong username and password. It only doesn't check integrity of dump taken, but it's another story.

Sign up to request clarification or add additional context in comments.

4 Comments

thanks Shader, the problem was that i am using a pipe after the mysql command to compress the output 'mysqldump -u usr -ppwd db | gzip -c > db.sql.gz' so i think that the result of $? concern the gzip command status not the first mysql one. that's it ?
Yes, that's correct. If we change the first line to mysqldump -h 192.168.1.10 -u user -pPaSSwOrD dbname | gzip > filename.sql.gz, then $? is referencing to gzip operation. To make it easier, split operation in two: make a dump (and check if it's successful), compress it to .gz (and check if necessary).
Other solution is to put set -o pipefail before mysqldump ... | gzip > .... In this case pipe will return an error if any of command fails.
thanks again, this last one resolve the problem to me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.