redirecting output in cron job not working
Cameron Simpson
cs at zip.com.au
Mon May 5 22:16:03 UTC 2008
On 05May2008 09:22, Mark Haney <mhaney at ercbroadband.org> wrote:
> Okay, I've seen this problem off and on for a year or two now and while
> it's not a problem, it's certainly annoying. I have several cron jobs that
> run every 3 hours over the course of a day. They are setup to direct
> output to the logs like this:
>
> /root/check_mounts.sh /mnt/m 10.5.0.100:/mnt/host-m/ 2>&1
>
> and for about six months, I was getting mails sent to me with empty output.
> Which I didn't need, since it is only supposed to email with a problem.
> However, for the last year or so, cron has been handling the jobs as it
> should, empty output doesn't get emailed to me. Now, however, it's doing
> it again.
No, it's probbly you.
Firstly, the about does not send output to a log, it sends stderr to
where stdout is going, which is still to cron's output capture.
You probably want this:
/root/check_mounts.sh /mnt/m 10.5.0.100:/mnt/host-m/ >SOME_LOGFILE 2>&1
Secondly, some conjecture: your "empty" messages were not empty, but
perhaps contained a single blank line or something like that.
Thirdly, it is worthwhile (though tedious) to exert some effort on
the cron jobs so that a successful run is silent. Not throw everythig
into a log file (though bulky output should probably go to such a place)
but to emit only messages about badness. That way, when things are bad
you _do_ get email and know that something should be investigated.
One strategy for discrepancy report I have found useful is to keep the
"raw" output in a log file, and email a "diff -u" between the log file
and the latest output, then rewrite the log with the latest output. This
way you have the whole discrepance in a handy log file, but only get
told when it changes. Useful for stuff that should be tidied up but not
yet got around to:
# you need to create /var/log/state, somewhere suitable
: ${TMPDIR:=/tmp}
logfile=/var/log/state/foo
tmpfile=$TMPDIR/foo.out
some-job.sh >"$tmpfile" 2>&1 || echo "exit $? from some-job.sh" >&2
diff -u "$logfile" "$tmpfile"
cat "$tmpfile" >"$logfile"
rm "$tmpfile"
Of course, all that should go in a utility script and be called from
cron like this:
/path/to/just-the-diff.sh /var/log/state/foo some-job.sh
so it is easy to do for lots of stuff.
Cheers,
--
Cameron Simpson <cs at zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/
We need a taxonomy for 'printing-that-is-no-longer-printing.'
- overhead by WIRED at the Intelligent Printing conference Oct2006
More information about the fedora-list
mailing list