Shell send error message

Asked

Viewed 751 times

4

I have a running cron task that calls a shell script. This sh file backs up the database and saves it to a folder on the server. So far it’s working.

I know that at the time of generating the backup and/or saving can happen an error. I would like to send this error to an email or generate some error log, something like that. Does anyone know how to do this?

Follows the script:

#!/bin/bash
# bkpDBData.sh
#
# gera um dump da base de dados, somente dos dados para um zip no servidor
#

# echo 'Iniciando backup SGI!';

local_save_backup=/local/salvar/backups
ano=`date +%Y`
path_complete=$local_save_backup'/'$ano

# verifica se existe a pasta do local_save_backup+ano
# se nao existe, entao cria a pasta
if [ ! -d $path_complete ]; then
    # echo "criar pasta";
    mkdir $path_complete
    # echo "pasta criada";
fi

mysqldump -h localhost -u user -psenha -x -e -t nome_da_base | gzip > $path_complete/`date +%Y%m%d_%H%M`_mysql.gz;

# echo 'Fim do backup SGI!';

1 answer

3


Any error messages will be sent to stderr, which is the standard shell error output. The strerr is identified by the number 2 when one wants to do some action with these mistakes.

So, suppose your cron makes a call like:

bkpDBData.sh

Let’s change this call to record errors in a log:

bkpDBData.sh 2>/var/log/bkdbdata.log

Then you can schedule a cron to pick up the log messages every 1 minute and send them to an email:

mail -s "bkpdbdata" [email protected] < /var/log/bkpdata.log
  • Very good, something I did not know and I will study about. I already did the test and generated an error (in case I put a wrong password to generate). I had no idea whether it was in the cron or in the shell that I had to make that check, but now it has opened me more mind to it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.