开发者

cron: sending output to file then EMAILing file to me

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开发者_StackOverflow社区 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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜