10

Why does cron require MTA for logging? Is there any particular advantage to this? Why can't it create a log file like most other utilities?

  • Bit of a nitpick, but I'm pretty sure cron doesn't need a MTA; unless you're shipping cron mails off to a different host, all it would need is a MDA (mail delivery agent). – a CVn Mar 11 at 9:24
19

Consider that the traditional "standard" way of logging data is syslog, where the metadata included in the messages are the "facility code" and the priority level. The facility code can be used to separate log streams from different services so that they can be split into different log files, etc. (even though the facility codes are somewhat limited in that they have fixed traditional meanings.)

What syslog doesn't have, is a way to separate messages for or from different users, and that's something that cron needs on a traditional multi-user system. It's no use collecting the messages from all users' cron jobs to a common log file where only the system administrator can see them. On the other hand, email naturally provides for sending messages to different users, so it's a logical choice here. The alternative would be for cron to do the work manually, and to create logfiles to each users' home directory, but a traditional multi-user Unix system would be assumed to have a working MTA, so implementing it in cron would have been mostly a futile exercise.

On modern systems, there might be alternative choices, of course.

13

I assume that by "logging" you mean storing the actual output of jobs. The running of jobs is already logged in the cron log in /var/cron/log (the path may differ between systems). There is no MTA required for this log.

A cron job is run as the user whose crontab the job is part of.

In the general case, there is no guarantee that this user is able to create files on the system (a user may not be an interactive user), especially not under the /var hierarchy where logs are usually created. The safest way to notify the user of errors and other output from a job is therefore to collect these and send them by email to the user. This would also allow the user to set up email redirection for the account to be able to see e.g. errors in their preferred location.

If the user wants to save the output of a job to file, they may do so with a simple redirection in the crontab:

0 */2 * * * "$HOME/scripts/myscript" >"$HOME/logs/myscript.log" 2>&1

This would run "$HOME/scripts/myscript" every second hour, on the hour, and would save all output to "$HOME/logs/myscript.log". No emails would be created by running this job as all output is redirected. Without the 2>&1, error messages would still be sent by email.

This allows the user to choose where the output goes.

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.