Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am running a half dozen different cron jobs from my hosting at Hostmonster.com. When a cronjob has been executed I receive an email with the output of the script.

The email comes in the format of:

From: Cron Daemon
Subject: Cron  /ramdisk/bin/php5 -c /home5/username/scheduled/optimize_mysql.bash

The problem with this is that the subject of the email makes it very hard to read which cronjob the email is pertaining to.

Is there a way to modify the subject of a cronjob email so that it's easier to read?

For example:

From: Cron Daemon
Subject: Optimize MySQL Database
share|improve this question
add comment

6 Answers

up vote 10 down vote accepted

On my systems (most Debian) all output, from a script/program called as a crontab-entry, is sended by email to the account@localhost who initiated the cron. These emails have a subject like yours.

If you want to receive an email, write a script that has no output on its own. But instead put all output in a textfile.

And with mail -s 'your subject' adress@where < textfile

you receive it the way you want.

share|improve this answer
    
Thanks Michèle. I was hoping to use Hostmonster's automatic email system but looks like this will be the solution I go with. Instead of recieving Automatic Cron emails controlled by my hosting I'll just send my own emails. –  justinl Aug 20 '09 at 8:40
add comment

Take over crond's responsibility for sending command output (or not if there isn't any) by piping output and stderr into 'mailx -E'. For example:

0 * * * * your-command 2>&1 | mailx -E -s "Descriptive Subject" $LOGNAME

Mailx's '-E' option is nice because, just like crond itself, it won't send a mail if there isn't any output to send.

share|improve this answer
    
What is the $LOGNAME variable here? –  Pratik Khadloya Jun 14 at 1:34
    
Is there a way to email only when the exit code of the command is non zero? –  Pratik Khadloya Jun 14 at 1:38
add comment

Another solution is to write a shell script with the subject line you want that calls the right command. In your example, this would be:

#Optimize_MySQL_Database.sh

/ramdisk/bin/php5 -c /home5/username/scheduled/optimize_mysql.bash

You can include your bin directory in the path by setting it in the crontab file.

share|improve this answer
add comment

TRY THIS--In the command line implement the following code---

/usr/local/bin/php -q /path /hostname/foldername/Page-You-want-to-execute \
   | **mail -s "*SUBJECT*" YOUR@MAIL.COM.**
  • This is just a referance to guide You
share|improve this answer
add comment

Or use the sh noop command (:)

0 9-17 * * 1-5    : Queue Summary; PATH=/usr/sbin qshape

The subject still looks kludgey, but at least it's descriptive and requires no extraneous scripts.

share|improve this answer
1  
Now that's a hack! (I like it.) –  Maxy-B Feb 24 '13 at 17:09
2  
Note that the space is important after the colon. –  Patrick Forget Feb 14 at 15:34
    
Is the title going to be "Queue Summary" ? And is the command "qshape" in this case? –  Pratik Khadloya Jun 14 at 1:20
    
Looks like qshape is a postfix command. My unix machine uses "mail" command. But is the qshape command relevant here? –  Pratik Khadloya Jun 14 at 1:30
    
This seemed like a great tip, but I just cannot get it to work. The command following the noop command is never executed. –  oligofren Jun 22 at 21:15
add comment

Pipe your cron job output to mail directly, and then you can fill in the subject line. the 2>&1 syntax sends any error output which would otherwise disappear.

mycmd 2>&1 | mail -s "mycmd output" myname
share|improve this answer
    
I'm doing this, but internal to the script cron calls because I want the subject to reflect the success or failure of the script being run. –  Tom Barron 2 days ago
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.