简体   繁体   English

在crontab centos中发送有关SQL错误的电子邮件

[英]Send Email on Sql error in crontab centos

I have a MySql script that runs over cron tab in centos linux. 我有一个MySql脚本,可在centos linux的cron选项卡上运行。

CALL some_stored_procedure();

there is a shell file which calls this MySql script with the name callsqlscript.sh 有一个外壳文件,该外壳文件使用名称callsqlscript.sh调用此MySql脚本。

mysql -uusername -ppassword db_name < /home/scripts/SqlScrip.sql  >> /var/log/SqlscriptsLog.txt

here is my crontab entry 这是我的crontab条目

*/1 * * * * root /opt/scripts/callsqlscript.sh 2>&1 | mail -s "SQL Errors" something@mydomain.com

I am getting the emails every minute. 我每分钟收到一封电子邮件。 I want email to be sent only if there any error running in SQL script. 我希望仅在SQL脚本中运行任何错误时才发送电子邮件。

Is there any way that I get the email only on errors. 有什么办法可以让我仅收到关于错误的电子邮件。 not every time 并非每次

Thanks 谢谢

Just use 1> /dev/null 只需使用1> /dev/null

The message of the email is composed of the command's output. 电子邮件的消息由命令的输出组成。 If there is none, no mail is sent. 如果没有,则不发送邮件。 The output is comprised of stdout and stderr 输出由stdoutstderr

So by changing your command to 因此,通过将命令更改为

/opt/scripts/callsqlscript.sh 1> /dev/null 

You redirect stdout to /dev/null (normal output) while keeping stderr (the errors) active. 您可以将stdout重定向到/dev/null (正常输出),同时保持stderr (错误)处于活动状态。 If anything is produced in stderr , that will get emailed to your email address 如果stderr产生了任何东西,它将通过电子邮件发送到您的电子邮件地址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM