6

On Ubuntu 18.04, man cron says

cron logs its action to the syslog facility 'cron', and logging may be controlled using the standard syslogd(8) facility

Also, the default configuration of cron is controlled by /etc/default/cron which is read by the init.d script that launches the cron daemon. This file determines whether cron will read the system's environment variables and makes it possible to add additional options to the cron program before it is executed, either to config‐ ure its logging or to define how it will treat the files under /etc/cron.d.

I added a job into /etc/crontab

* * * * * t ( date && echo $PATH && date )

I try to look up the output of the job, and why couldn't I find it in syslog? Thanks.

$ less /var/log/syslog
...
Nov  3 12:21:01 ocean CRON[23992]: (t) CMD (( date && echo $PATH && date ) )
Nov  3 12:21:01 ocean CRON[23985]: (CRON) info (No MTA installed, discarding output)


$ cat  /etc/default/cron
# This file has been deprecated. Please add custom options for cron using
# $ systemctl edit cron.service
# or
# $ systemctl edit --full cron.service
Tim
  • 101,790
  • Tim: you're on a modern distro, consider using systemd timers rather than cron for your purposes. – filbranden Nov 03 '18 at 17:00
  • @FilipeBrandenburger Thank. That is surprising. I never know cron is outdated and replaced by systemd timers. Would like to read more references that back up your comment. – Tim Nov 03 '18 at 17:10
  • See https://wiki.archlinux.org/index.php/Systemd/Timers which says "Timers can be used as an alternative to cron." That wiki is a great reference too (most or almost all if it is not really specific to Arch Linux, so you can apply most advice and recipes elsewhere.) – filbranden Nov 03 '18 at 17:25
  • It's not that cron is obsolete... But I'd argue it's never been a great system to start with and it has lots of idiosyncrasies (e.g. $PATH, output by mail) that make it annoying to use in 2018... So I'd say instead of learning to use cron, spend time learning how to use something more modern and flexible, with more options to schedule jobs and integrated logging (through the journal), etc. – filbranden Nov 03 '18 at 17:28
  • 2
    There still Unix versions without systemd and people using Linux without systemd. +1 – Rui F Ribeiro Nov 03 '18 at 18:26
  • First line of the post says: "On Ubuntu 18.04, ..." So that's pretty much a systemd distro. – filbranden Nov 04 '18 at 00:26
  • 1
    https://unix.stackexchange.com/questions/278564/cron-vs-systemd-timers – Tim Nov 04 '18 at 02:25
  • https://www.reddit.com/r/linuxadmin/comments/7ea2mj/cron_vs_systemd/ – Tim Nov 04 '18 at 12:53

1 Answers1

11

cron logs its actions to syslog, with a level of detail dependent on the configured log level. This doesn’t include jobs’ output.

Regarding the latter, the manpage says

When executing commands, any output is mailed to the owner of the crontab (or to the user named in the MAILTO environment variable in the crontab, if such exists).

Since you don’t have a configured MTA, output is discarded, as indicated in your logs.

If you want to configure an MTA, the MTA packages in Debian and derivatives come with nice installation scripts which can configure a simple setup for you; sudo apt install postfix and follow the prompts.

Alternatively, you can redirect the output of your cron jobs to the system log by using logger: pipe the output of your jobs to logger -p cron.info (for regular output) and logger -p cron.err (for errors).

The reason cron expects to send output by email is that cron jobs’ output has a granularity which doesn’t match the system logger’s (logs aren’t stored per user). Each user of a system can set up cron jobs, but users don’t necessarily have access to the system logs, and they might not want all other users with access to those logs to see their jobs’ output. When cron was designed, email was a sensible way to get the output of a cron job to the user who managed it: it was expected that Unix systems had a working email setup. Nowadays it’s less obvious, not because the suitability of email has changed, but because its availability has. On systems with working email, it still makes sense to use that for cron output; alternatively, jobs can output to per-user log files (with specific redirections in each job).

systemd timers don’t suffer from this issue: user timers log to the private, per-user journal.

Stephen Kitt
  • 434,908
  • Thanks. cronie's manpage says" The syslog output will be used instead of mail, when sendmail is not installed." Does Debian's cron not use syslog in the same case? – Tim Nov 03 '18 at 16:46
  • 1
    Tim indeed quoted this part of the manual, some hours before this question, in https://unix.stackexchange.com/questions/479517/ . – JdeBP Nov 03 '18 at 16:48
  • @Tim the log message describes what cron is doing: it discards the output if no MTA is available. As I mentioned before, you can’t read other systems’ cron documentation and expect it to apply to Debian’s versions (and vice versa). – Stephen Kitt Nov 03 '18 at 17:02
  • Thanks @JdeBP, I didn’t take the time to read all of Tim’s cron questions today before answering this one. – Stephen Kitt Nov 03 '18 at 17:03
  • (1) Is MTA the same thing as sendmail? How shall I configure it? (2) Does cron -s work effectivey the same as I add redirection to the command in my cron job? (3) Since my ubuntu is running cron, how can I run cron -s? – Tim Nov 03 '18 at 17:04
  • @Tim MTA is Mail transfer agent, postfix, sendmail, .exim...syslog does not handle emails. – Rui F Ribeiro Nov 03 '18 at 18:27
  • @Tim I’ve attempted to address your underlying questions in my update. You can’t use cron -s because you’re not using cronie. There are multiple implementations of cron, each with different features; you’re reading the documentation for cronie (as used in Fedora etc.), and it doesn’t apply to the version of cron used in Ubuntu. You can however approximate cron -s by redirecting to logger. – Stephen Kitt Nov 03 '18 at 22:05
  • Thanks. Sorry I mix cronie with vixie. (1) I was wondering in what sense "cron jobs’ output has a granularity which doesn’t match the system logger’s." (2) "when cron was designed, email was a sensible way to get the output of a cron job to the user who managed it." Nowadays, is email still a sensible way, or syslog is more sensible, or shell redirection, or something else (like systemd timers)? – Tim Nov 03 '18 at 22:17