cron: sending output to file then EMAILing file to me - email

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

Related

Problems with using uuencode on RHEL to send mail attachment

I have a unique situation where I'm going from an AIX platform to RHEL for a vendor supported application. Within the application, I can send a command (limited to the amount of characters I can send) that will email me results of a print job.
To do this in AIX, I'm passing the following command:
uuencode temp.txt | mail -s "test" email#address.com
This sends standard output to the email as an attachment named temp.txt. Temp.txt doesn't exist on the AIX server.
I have downloaded the rpm for uuencode for Linux, but it doesn't work in the same fashion. It sends an email with what looks like garbage (could be missing MIME header).
I'm looking for a similar command. To simulate outside of the application, I use the following in AIX:
echo "testing mail" | uuencode temp.txt | mail -s "test" email#address.com
I know mailx -a <filename> would work if I had a filename, unfortunately I'm working with standard output vs a file.
Any help will be appreciated.

How to add X-header to unix mailx or add attachment to usr/sbin/sendmail

I am attempting to add the x-header X-APP-VOLT: Yes to the header of my email with a .tar attachment. I only have access to usr/sbin/sendmail and mailx. I do not have root access so I can't download other versions of mailx or mutt.
I can add the x-header to usr/sbin/sendmail using the below code, but I can't figure out how to add the .tar attachment.
/usr/sbin/sendmail -i -- toemail << END
To: toemail
Subject: Test
X-APP-VOLT: Yes
Hope this works! END
I can attach a .tar file to mailx using the the below code, but I can't figure out how to add a x-header. My mailx also does not have the -a option.
cat file | uuencode filename | mailx -s "Test" toemail
Thank you
One way is to construct your input in a temporary file:
cat > tmpfile$$ << END
To: toemail
Subject: Test
X-APP-VOLT: Yes
Hope this works!
END
uuencode filename < file >> tmpfile$$
/usr/sbin/sendmail -i -- toemail < tmpfile$$
Also, I usually use sendmail's -t flag in this case, rather than repeating the recipient:
/usr/sbin/sendmail -i -t < tmpfile$$
If you don't want to use a temporary file, if you want to use a pure pipeline, you can use ( ) to create a subshell to do the construction:
(
echo "To: toemail"
echo "Subject: Test"
echo "X-APP-VOLT: Yes"
echo
echo "Hope this works!"
echo
uuencode filename < file
) | /usr/sbin/sendmail -i -t
(Of course, these days most recipients will probably find it easier to deal with MIME attachments rather than uuencode. It's pretty straightforward to create MIME attachments with a shell script, also.)

Mailx - Daily email of log file to list of recipients

I was given a few example usages of mailx and asked to come up with another by my manager.
I have a program that will be left running. Once a day, we want to send the log file it generates to a list of recipients.
This was the closest example I received
00 11 * * * unset noclobber;cat /home/emailList.txt /tools/temp/summary.csv | mailx -t -s "Report `/bin/date`" -r person#company.com > /home/file.log
Seems like I want something like:
MyProgram | mailx -t -s "Report `/bin/date`" /home/emailList.txt -r person#company.com
But here, I'm not sending it once a day. This sends at MyProgram closing, right? I'm also sending MyProgram's output, not it's log file.
Thanks in advance.

inserting system date into subject field of ssmtp email

I'd like to know the syntax for inserting the current date/time into the subject line of an email sent by ssmtp.
I've got a cronjob emailing the tail of my syslog whenever the system reboots.
Here is the the cronjob:
#reboot tail -1000 /var/log/syslog | mail -s "the system rebooted, here's the syslog" address#gmail.com &> /dev/null
Is there a simple way of inserting the system date into the subject line field? I haven't found a way to add it.
You should be able to make it print the current date and time by inserting $(date) into your subject line string.
Try:
#reboot tail -1000 /var/log/syslog | mail -s "$(date): the system
rebooted, here's the syslog" address#gmail.com &> /dev/null

procmail not piping e-mail content to a file

I have a postfix server and procmail installed and working.
The problem is when I try to output the content of an e-mail to a file.
I have the following script:
/var/log/user1/fooscript.sh
#!/bin/bash
echo "Trying to get e-mail" > success.txt
echo $1 >> success.txt
/var/log/user1/.procmailrc
VERBOSE=off
PMDIR=$HOME/.procmail
LOGFILE=$PMDIR/procmail.log
INCLUDERC=$PMDIR/rc.filters
/var/log/user1/.procmail/rc.filters
:0
* ^From:(.*\<)?(test#gmail\.com)\>
| /var/log/user1/fooscript.sh
After sending an e-mail, /var/log/user1/.procmail/rc.filters
contains:
From test#gmail.com Thu Jul 18 05:08:13 2013
Folder: /var/log/user1/fooscript.sh 513
but the success file only shows:
Trying to get e-mail
(empty line)
I've chmod 777 all files and directories, so don't think its a permissions issue.
Any help would be greatly appreciated.
Your script gets the message via standard input (STDIN). Try:
#!/bin/bash
echo "Trying to get e-mail" > success.txt
# append data read from STDIN to success.txt file
cat >> success.txt
BTW for more complicated scripts use custom lock to avoid running two scripts in parallel:
:0 w :fooscript.lock
* ^From:(.*\<)?(test#gmail\.com)\>
| /var/log/user1/fooscript.sh