cron:将输出发送到文件,然后将EMAIL文件发送给我 [英] cron: sending output to file then EMAILing file to me

查看:179
本文介绍了cron:将输出发送到文件,然后将EMAIL文件发送给我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我想记录输出可以使用简单的管道重定向来完成文件的操作:

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

但是,我不知道如何将文件(或简称其内容)邮寄到我在单独的电子邮件到me@somewhere.com



另外,是否有一种方法来动态创建日志文件名,基于日期,以便日志名称比如%Y%m%d.task1.log?



哪个前缀是日期?



我在Ubuntu 10.0.4上运行LTS

解决方案

如果您的系统有一个工作 / usr / bin / sendmail (不必是 sendmail sendmail ,大多数邮件服务器提供 / usr / bin / sendmail 包装器脚本)然后您可以使用邮件(1)实用工具发送邮件:

  echohello world| mail -s hello me@example.com 

mail(1)是非常原始的;没有MIME文件附件,你被困在明文上。



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

  echohello world| mutt -a任务* .log  -  me@example.com 

至于给出日志文件日期:

  $ echo嗨> $(日期+%Y%m%dlog.txt)
$ cat 20110328log.txt
hi
$

所以,尝试这样:

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


I am writing a series of cron jobs. I want each task to log its output to file, and then I want the contents of the file mailed to me at say me@somewhere.com

I think logging the output to file can be done using simple pipe redirection like this:

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

However, I am not sure how to mail the files (or simply their contents) to me in seperate emails to me@somewhere.com

Also, is there a way to dynamically create the log file names, based on the date, so that the log names would be something like %Y%m%d.task1.log ?

Where the prefix is the date ?

I am running on Ubuntu 10.0.4 LTS

解决方案

If your system has a working /usr/bin/sendmail (doesn't have to be sendmail sendmail, most mail servers provide a /usr/bin/sendmail wrapper script) then you can use the mail(1) utility to send mail:

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

mail(1) is pretty primitive; there's no MIME file attachments, you're stuck with plaintext.

If mutt(1) is installed, you can use MIME to attach files:

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

As for giving the logfiles dates:

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

So, try this:

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

这篇关于cron:将输出发送到文件,然后将EMAIL文件发送给我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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