简体   繁体   English

如果cronjob备份失败,则执行curl命令

[英]Execute a curl command if cronjob backup fails

I have a cronjob that takes backup of my DB every 10 min. 我有一个cronjob,每10分钟备份一次我的数据库。 I want to receive an SMS whenever the backup fails. 每当备份失败时,我都希望收到一条SMS。

Example: 例:

*/1 * * * * sudo /usr/local/sbin/mysql.sh 2>&1 | curl 'http:www.smsapi.com/user=username&pass=password&text=$errormsg'

Currently it's not sending any SMS. 目前它没有发送任何短信。 How can I append error message to curl? 如何附加错误消息以卷曲? (I have also tried this for Email instead of SMS, but that too does not work). (我也尝试过用电子邮件代替短信,但这也行不通)。

Mail: 邮件:

mysql.sh 2>&1 | mail -s "Mysql backup" -E user@host

cUrl: 卷曲:

mysql.sh 2>&1 | curl "http:www.smsapi.com/user=username&pass=password&text=`cat -`"

Please note: double quotes instead of apostrophes and backticks with a command to convert std input to a parameter. 请注意:用双引号代替撇号和反引号,使用命令将std输入转换为参数。 The conversion is rather primitive, it doesn't URL-quote special characters. 转换是相当原始的,它不会用URL引用特殊字符。

You need to use the double-pipe command || error-handler 您需要使用双管道command || error-handler command || error-handler for the error-handler to be called only in case the command fails. command || error-handler程序,仅在命令失败的情况下才调用错误处理程序。

I suggest you reverse the logic command && success-handler and use a service such as WDT.io for the success-handler to alert you in case of a missing success (aka failure). 我建议您反转逻辑command && success-handler并对command && success-handler使用WDT.io之类的服务,以在缺少成功(又称失败)的情况下向您发出警报。 The reason is that the cron job itself can fail to run which you'd never find out about when using an error-handler. 原因是cron作业本身可能无法运行,而使用错误处理程序时您将永远无法找到它。

As for your second question, how to append the error, you can access the previous return code using $? 至于第二个问题,如何附加错误,您可以使用$?访问先前的返回代码$? .

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

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