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

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.

  • 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

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.

  • 6
    Now that's a hack! (I like it.)
    – Maxy-B
    Feb 24 '13 at 17:09
  • 9
    Note that the space is important after the colon. Feb 14 '14 at 15:34
  • 1
    Is the title going to be "Queue Summary" ? And is the command "qshape" in this case? Jun 14 '14 at 1:20
  • Looks like qshape is a postfix command. My unix machine uses "mail" command. But is the qshape command relevant here? Jun 14 '14 at 1:30
  • 3
    FYI, the subject of the email will be Queue Summary; PATH=/usr/sbin qshape
    – Akom
    Dec 11 '18 at 15:18

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
  • 2
    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
    Jul 10 '14 at 14:29

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
    What is the $LOGNAME variable here? Jun 14 '14 at 1:34
  • Is there a way to email only when the exit code of the command is non zero? Jun 14 '14 at 1:38
  • 2
    @PratikKhadloya, from crontab(5): Several environment variables are set up automatically by the cron(8) daemon. SHELL is set to /bin/sh, and LOGNAME and HOME are set from the /etc/passwd line of the crontab's owner. PATH is set to "/usr/bin:/bin". HOME, SHELL, and PATH may be overridden by settings in the crontab; LOGNAME is the user that the job is running from, and may not be changed.
    – lingfish
    Jun 27 '15 at 3:32
  • 2
    There are multiple mailx implementations; only some of them offer this -E option. On Ubuntu you want the bsd-mailx or heirloom-mailx packages; the mailutils package contains a Gnu mailx command with a different -E.
    – Smylers
    Oct 28 '15 at 15:53

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:


/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.


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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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