Nate
Nate

Reputation: 28454

Cron output is emailed as a file instead of showing in the body?

I've setup crons through cPanel for a long time and the output is always emailed to me. However, I need a cron to run with root permissions and so I added something like this to crontab:

5 0,12,8,20 * * * php /path/to/file.php 2>&1 | mail -s "my cron job" [email protected]

The command executes and I receive an email, but the output is attached as a file ironically named noname.

How can I get the output to show up in the body of the email instead of in an attached file?

Upvotes: 2

Views: 527

Answers (2)

ehime
ehime

Reputation: 8415

I would highly recommend using mutt instead

5 0,12,8,20 * * * php /path/to/file.php 2>&1 |mutt -s "your cron job" -- [email protected]

or if you're really set on using whatever your default mail is, possibly use an intermediary file

5 0,12,8,20 * * * php /path/to/file.php 2>&1 /tmp/outfile; mail -s "your cron job" [email protected] < /tmp/outfile; rm -f /tmp/outfile

Upvotes: 0

Appleman1234
Appleman1234

Reputation: 16116

First can you confirm which mail program you are using and which cron program you are using ? E.g. by default most cron software restricts the PATH, unless you have it set at the top of the crontab.

/bin/mail at least from GNU mailutils doesn't support sending attachments without help from another program (uuencode). /bin/mailx from Heirloom which your system may be using supports attachments, and will happily convert mail sent in non interactive mode that contains illegal byte sequences (non ASCII), from text to an octet stream file attachment as per the Changelog

You can get the output to show up in body instead of attached file by

  1. Changing which mail software you use
  2. Removing any illegal byte sequences from your output before piping it to mail. See the existing StackOverflow Question Best way to convert text files between character sets? for how to do that.

Upvotes: 1

Related Questions