
Cronic - A cure for Cron's chronic email problem - duck
http://habilis.net/cronic/
======
there
In a dozen or so years of administrating many different Unix machines, I've
never had cron email be a problem and I get a few (useful) emails from various
cron scripts every day.

 _This interacts badly with many unix commands, which often send status info
to standard out. Some commands have a quiet options, but that can turn off all
error output too._

Maybe it's that I've mostly been administrating BSD machines, and so most of
the tools follow old Unix guidelines like not printing anything unless it's
necessary, echoing errors to stderr, using proper exit statuses, etc.
(<http://fmg-www.cs.ucla.edu/geoff/interfaces.html>) I think it's a
GNUism/Linuxism that commands are overly chatty, writing junk all over stdout
(like author/license information - do we need to see this every single time?),
and using ANSI colors by default.

~~~
ajross
Which tools are you talking about? The standard software suite is basically
identical between the systems. And what differences there are lie in the
implementations of the core stuff, whose behavior is specified by POSIX for
the most part.

I'm sure there are exceptions somewhere. But in something you'd throw into a
cron job? Frankly, that seems like a very weird snipe.

~~~
emmelaich
I'm guessing _ls_ which colors; perhaps _keychain_ (very colorful output).

I see less of the license stuff these days but no doubt there's a few around.

~~~
ciupicri
On Red Hat based systems, ls is an alias for _ls --color=auto_ which means
that colors are used only on terminals, not pipes or files.

~~~
ajross
And no one should ever be using ls in a shell script anyway. Shell globbing
and find are the proper tools.

------
joeyh
I added a simplified version of chronic to moreutils a while back.
<http://joeyh.name/code/moreutils> (also packaged in some major linux distros)

~~~
kree10
It's not just simplified, but more secure. Your version doesn't write to
predictable filenames in /tmp like the original does.

------
rhizome
I don't like spurious cron emails either, that's why I put effort into writing
decent scripts.

------
veyron
There are more "fixes" for cron than I can count. I interpret this more as a
fundamental misunderstanding of unix utilities and standards.

Utilities should not print out anything that the user can safely ignore. If
you want to print out more info, add a VERBOSE mode (-v or -vv ...).

------
repiret
Writing programs like this must be a rite of passage. About a year ago I wrote
one (<https://github.com/mlaiosa/cronwrap>). One day when I googled "cronwrap"
to try to find the github page, there was a gazillion hits of other programs
that also did the same thing. I looked at a couple and I still like mine more
- but I also have a moderate case of not-invented-here syndrome.

~~~
thwarted
Don't we all?

<https://github.com/thwarted/shuck>

------
jcampbell1
Since this is hackernews, can someone explain the rational behind putting the
2>&1 after the > ?

I know it works, but this reads to me: command > file.txt 2>&1 "write the
output of command to file.txt and then map the error output to stdout"

It makes so much more sense to write: command 2>&1 > file.txt

There is clearly something in my brain that is confused about how redirection
works.

~~~
akkartik
Siblings have already responded to this well, so I'll just add an example:

    
    
      $ ls x
      ls: cannot access x: No such file or directory
      $ ls x >/dev/null 2>&1
      $ ls x 2>&1 >/dev/null
      ls: cannot access x: No such file or directory
    

The first command shows us trying to list a non-existent file, raising an
error. The second sends stderr to stdout before sending stdout to null,
suppressing all output. The third sends the error to stdout; any output on
stdout would have been suppressed (can you come up with a way to verify this?)

~~~
jrockway
_can you come up with a way to verify this?_

    
    
       perl -E 'say q{STDOUT!}; say {*STDERR} q{STDERR!}'
    

(q{...} quoting to avoid ugliness of quotes inside quotes.)

------
dfrankow
I have this problem, I only want to be emailed upon failure. However, the
crontab of the account is shared by several different jobs run by different
people, and I don't have control over that. Perhaps I could set the email in
the crontab line?

* * * * * MAILTO=me@example.com blahscript.sh

~~~
Dobbs

        blahscript.sh | mail -s "Subject" me@example.com

~~~
ralph
That always sends an email, even if the body is empty.

------
lucian1900
Wouldn't it be simpler to just only direct stdout to /dev/null and let cron
capture stderr?

------
zobzu
That's not too bad and too be honest I think I'd like to see it _inside_ cron.
So we'd just..

* * * * * blahscript.sh

and it'd do what cronic does without any extra. I know i know the "one tool
one job", but i believe this job is really cron's job (word play unintended)

------
cslacasse
I use a wrapper similar to this. Sometimes you just need to call a script
someone else wrote that you have no control over the output. I never want an
email unless the script results in a non-zero rc.

------
SourPatch
There are many different cron implementations. Do none of them check exit
status?

~~~
reidrac
All of them.

cron sends a mail if: a) there's output (stdout or stderr) or b) the command
exit code isn't 0.

So I really don't understand the post when it says "Now when your cron job
fails, you will never know about it.".

Well, if you script fails and still returns 0, your script is broken. If you
script outputs stuff when it's not supposed to, it can't run from cron.

------
Jemm
Oh man. I was all excited reading the headline until I realized that you
weren't talking about a cure for Crohn's.

~~~
btilly
One exists. Hookworm aka Helminthic therapy.

Unfortunately the FDA has ruled that this treatment would require approval.
The approval process is expensive, and there is no way that anyone can recoup
their expenses for doing so. Therefore this treatment will never be approved
in the USA.

But I know someone who had severe Crohn's disease who went to Canada to
receive a mail order of hookworm. Thanks to that he's been totally off drugs
for over a year, and shows no signs of the disease.

------
kbronson
I like how the web page switches to two-column layout if it has enough screen
space.

~~~
ibotty
now that's nice. i did not notice, as it depends on javascript. but very nice!
something to copy i guess ;).

(that alone is worth the article. the "real" content is just misunderstanding
of unix i'm afraid)

