【问题标题】:cron: sending output to file then EMAILing file to mecron:将输出发送到文件,然后通过电子邮件将文件发送给我
【发布时间】:2011-07-24 06:19:56
【问题描述】:

我正在编写一系列 cron 作业。我希望每个任务都将其输出记录到文件中,然后我希望通过 me@somewhere.com 将文件的内容邮寄给我

我认为将输出记录到文件可以使用简单的管道重定向来完成,如下所示:

30 0 * * * /path/to/script1 > task1.log
30 1 * * * /path/to/script2 > task2.log

但是,我不知道如何通过单独的电子邮件将文件(或只是它们的内容)邮寄给我@somewhere.com

另外,有没有办法根据日期动态创建日志文件名,以便日志名称类似于 %Y%m%d.task1.log ?

前缀是日期在哪里?

我在 Ubuntu 10.0.4 LTS 上运行

【问题讨论】:

    标签: email bash ubuntu cron


    【解决方案1】:

    如果您的系统有一个有效的/usr/bin/sendmail(不一定是sendmail sendmail,大多数邮件服务器都提供一个/usr/bin/sendmail 包装脚本),那么您可以使用mail(1)发送邮件的实用程序:

    echo "hello world" | mail -s hello me@example.com
    

    mail(1) 非常原始;没有 MIME 文件附件,您只能使用纯文本。

    如果安装了mutt(1),则可以使用MIME附加文件:

    echo "hello world" | mutt -a task*.log -- me@example.com
    

    至于给出日志文件的日期:

    $ echo "hi" > $(date "+%Y%m%dlog.txt")
    $ cat 20110328log.txt              
    hi
    $
    

    那么,试试这个:

    30 1 * * * /path/to/script2 > $(date "+\%Y\%m\%dlog.txt") && mutt -a $(date "+\%Y\%m\%dlog.txt") -- me@example.com
    

    【讨论】:

    • 在 cron 脚本中使用之前,需要对 '%' 字符进行转义。这给我今天带来了各种各样的问题;)
    • @ommpahloompah,绉纱,对不起。 :(
    猜你喜欢
    • 2014-05-18
    • 2013-12-18
    • 1970-01-01
    • 1970-01-01
    • 2018-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-04
    相关资源
    最近更新 更多