[英]Checking mail subject from Bash or Python
I have many cron
jobs running on various servers, I want to check the status of the jobs via mail using command line. 我有许多在各种服务器上运行的
cron
作业,我想使用命令行通过邮件检查作业的状态。 The only way seems to me is to extract the subject or body of the mail the jobs sent. 在我看来,唯一的方法是提取作业发送的邮件的主题或正文。
cron
will send mail regardless of the success or failure. 无论成功与否,
cron
都会发送邮件。
For example, upon success cron
will send a mail with subject: 例如,成功完成后,
cron
将发送主题为:
Done..succeeded
If failed: 如果失败:
Not done..Failed
The cron
jobs run at specific times eg lets take 10:00, 16:00 and 22:30 everyday. cron
作业在特定时间运行,例如,每天花费10:00、16:00和22:30。
I have tried curl
and urllib2
but could not get desired result. 我已经尝试过
curl
和urllib2
但是无法获得想要的结果。 Also note that i can not make any modification to cron
itself, the only option is to check the gmail. 另请注意,我无法对
cron
本身进行任何修改,唯一的选择是检查gmail。
So how can i check my gmail from bash
or python
to extarct the subject so that i get get an idea of the cron
job? 那么,如何才能从
bash
或python
检查我的gmail以突出显示该主题,以便使我对cron
工作有所了解?
You can use imaplib
to talk to Gmail with the IMAP protocol. 您可以使用
imaplib
通过IMAP协议与Gmail通讯。 This is the same protocol that you would use to have Gmail work with eg Thunderbird, so it's fairly easy to retrieve messages rather than dealing with scraping often-changing HTML. 这是与Gmail配合使用(例如Thunderbird)时所使用的协议,因此检索邮件而不是处理经常更改的HTML抓取非常容易。
I had some python code that used to check gmail for me. 我有一些用于为我检查gmail的python代码。 I was going to post it but I tried it out and it no longer works for me.
我本来打算发布它,但我尝试了一下,但它不再对我有用。 I haven't used it for about a year and Gmail has updated their auth to use oauth2 which is more secure.
我已经使用了大约一年了,Gmail已将其身份验证更新为使用更安全的oauth2。 You may be able to get imaplib to work as well but you will need to enable less secure apps on your gmail account.
您也许可以使imaplib正常运行,但需要在gmail帐户上启用安全性较低的应用。 If I have time to tweak it and get it working I will post it.
如果我有时间对其进行调整并使它正常工作,我会发布它。 In the meantime this link looks like a pretty good walk-through of how to get it working.
同时,此链接看起来像一个很好的演练,介绍了如何使其工作。 https://github.com/google/gmail-oauth2-tools/wiki/OAuth2DotPyRunThrough
https://github.com/google/gmail-oauth2-tools/wiki/OAuth2DotPyRunThrough
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.