Cron作业 - 如何将输出文件发送到电子邮件 [英] Cron Job - How to send an output file to an email

查看:182
本文介绍了Cron作业 - 如何将输出文件发送到电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在crontab中有这行:

  * * * * * / var / www / dir / sh / mysql_dumb。 sh | mail -smysql_dumpexample@mail.com 

(每分钟只有一个示例)



所以,一切正常,但电子邮件为空。



更新: p>

mysql_dumb.sh的输出是一个 *。sql 文件,它们将文件保存在一个目录中。



如何发送一个副本(* .sql文件)从这个输出 - > mysql_dumb.sh 到我的电子邮件? / p>

mysql_dumb.sh:

 #!/ bin / bash 
PATH = / usr / bin:/ bin
SHELL = / bin / bash
/ usr / bin / mysqldump -u USER -pPASS DATABASE> / var / www / dir / backup / backup_DB_`date +%d_%m_%Y`.sql


解决方案

如果脚本报告错误,他们可能会去 stderr ,但你只重定向 stdout 。您可以通过向命令中添加 2>& 1 来重定向 stderr

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


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)

So, all works fine, but the email is empty.

UPDATE:

The output from mysql_dumb.sh is a *.sql file and they save the file in a directory.

How can I send a copy (*.sql file) from this output -> mysql_dumb.sh to my email?

mysql_dumb.sh:

#!/bin/bash
PATH=/usr/bin:/bin
SHELL=/bin/bash
/usr/bin/mysqldump -u USER -pPASS DATABASE > /var/www/dir/backup/backup_DB_`date +%d_%m_%Y`.sql

解决方案

If the script is reporting errors, they may be going to stderr, but you're only redirecting stdout. You can redirect stderr by adding 2>&1 to the command:

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

这篇关于Cron作业 - 如何将输出文件发送到电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆