58

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
  • 7
justinl
  • 703
  • 1
  • 6
  • 9

6 Answers6

65

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. – Patrick Forget Feb 14 '14 at 15:34
  • 1
    Is the title going to be "Queue Summary" ? And is the command "qshape" in this case? – Pratik Khadloya Jun 14 '14 at 01: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 '14 at 01: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 '14 at 21:15
  • Tried this on CentOS release 6.8 and on Ubuntu 14.04.5 LTS and it worked like a charm. Accepted answer is probably better architecturally, but this gets the job done and doesn't require you to wrap your scripts. I would also highly suggest looking at `qshape 2>&1 | mail -s "Queue Summary" -E` which sends an email only if it's non-zero length. (`mail -E`) Also see http://unix.stackexchange.com/questions/13326/how-to-pipe-output-from-one-process-to-another-but-only-execute-if-the-first-has – razzed Jan 01 '17 at 22:19
  • 4
    FYI, the subject of the email will be `Queue Summary; PATH=/usr/sbin qshape` – Akom Dec 11 '18 at 15:18
  • I would like to sneak in some end-of-string character after `Quere Summary` to shorten the subject line. I have tried `\u0003`, that is, `=?UTF-8?Q?=03?=`, but that is shown as a space in the email header when received. Any other ideas? – bers Apr 07 '20 at 16:28
27

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
Mark Harrison
  • 795
  • 2
  • 11
  • 20
  • 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
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.

Alexis Huxley
  • 141
  • 1
  • 3
  • 2
    What is the $LOGNAME variable here? – Pratik Khadloya Jun 14 '14 at 01:34
  • 1
    Is there a way to email only when the exit code of the command is non zero? – Pratik Khadloya Jun 14 '14 at 01:38
  • 3
    @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 03:32
  • 3
    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
12

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
  • 3
Michèle
  • 340
  • 2
  • 4
  • 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 08:40
  • Rather than using a temp file (which creates a possible race condition) you could simply pipe the output of a command to the mail command. – dummzeuch Jun 28 '22 at 08:15
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
  • 108,414
  • 18
  • 172
  • 242
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
  • 7,355
  • 16
  • 54
  • 72
sudhu
  • 1