62

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
miyuru
  • 139
justinl
  • 743

6 Answers6

70

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.

28

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
14

On my systems (most Debian) all output, from a script/program called as a crontab-entry, is sent 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.

Scz
  • 103
Michèle
  • 460
14

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.

2

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.

EEAA
  • 110,608
0

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
slm
  • 8,010
sudhu
  • 1