Cron Job - How to send an output file to an email

前端 未结 4 1418
情书的邮戳
情书的邮戳 2020-12-13 13:43

I have this line in crontab:

* * * * * /var/www/dir/sh/mysql_dumb.sh | mail -s \"mysql_dump\" example@mail.com

(every minute only a sample)

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-13 14:12

    Look at the last line of mysql_dumb.sh: /usr/bin/mysqldump -u USER -pPASS DATABASE > /var/www/dir/backup/backup_DB_`date +%d_%m_%Y`.sql

    The > is redirecting the output of mysqldump to the file /var/www/dir/backup/backup_DB_date +%d_%m_%Y.sql

    Do you want to store a backup of the database locally?

    If not, take out the the > /var/www/dir/backup/backup_DB_`date +%d_%m_%Y`.sql and put the crontab entry back to

    * * * * * /var/www/dir/sh/mysql_dump.sh 2>&1 | mail -s "mysql_dump" example@mail.example

    If you do want a copy of the file locally, I would suggest using tee which will write the output to the file and put the output back out on stdout, which will later be picked up by crontab.

    I would change the last line of mysql_dumb.sh to be: /usr/bin/mysqldump -u USER -pPASS DATABASE | tee /var/www/dir/backup/backup_DB_`date +%d_%m_%Y`.sql

    Again I would change the crontab entry back to: /usr/bin/mysqldump -u USER -pPASS DATABASE > /var/www/dir/backup/backup_DB_`date +%d_%m_%Y`.sql

    The advantage here is mail can read the information from stdout and isn't dependent on the file being written and then read correctly. While that may be a small difference, in my experience using tee will be more reliable.

提交回复
热议问题