0

The manpage of cron says

When executing commands, any output is mailed to the owner of the crontab (or to the user specified in the MAILTO environment variable in the crontab, if such exists). Any job output can also be sent to syslog by using the -s option.

I am trying to understand why the cron daemon makes efforts about recording output when executing a cron job. It looks to me that using redirection in the job will also work?

Does "any output" include both

  • the output generated by the job executed by cron daemon
  • and the output generated the cron daemon itself?

If

  • "any output" is only the output generated by the job executed by cron daemon and not the output generated by the cron daemon itself, and

  • we only care the content of the output, not which file the output will be written to,

can we just specify in the job line in cronab file to redirect the output of the command(s) in the job into any file, instead of using cron's loggin/email features (e.g. option -s and the likes)?

30 18 * * * rm /home/someuser/tmp/* > /home/t/mycron.log  2>&1

Thanks.

  • If the job itself redirects to a file, and that redirection fails, would you not want cron to tell you about it? – Kusalananda Nov 3 '18 at 14:27
  • Yes. I do. Can failure of redirection be recorded by 2>&1? – Tim Nov 3 '18 at 14:53
  • The shell that executes the redirection would output an error message to its standard error stream. If that error stream also fails to redirect (maybe because it's redirected to the same place as standard output), then this would have to be conveyed to the user somehow. This would be intercepted by cron and emailed to the user of the job. But there are other cases too, such as someone scheduling a script, and the script getting deleted. That too has to be reported somehow. – Kusalananda Nov 3 '18 at 15:16
  • Only if that directory exists and if the user running the job can truncate and write to the file. If not, then what should cron do? Well, report the failed redirection via email seems like a good suggestion. – Kusalananda Nov 3 '18 at 15:21
1

"Any output" here means "anything sent to standard output or standard error output by the command(s) in the job."

If the command to be executed includes a redirection of standard output to a file, there will be no standard output emanating from that command as a whole, since it will all be redirected to the file.

The output generated by the job is considered to be the property of whoever user owns the job; the log messages about the job execution (i.e. what was executed, when, on whose account, and what was the result code of the job) created by the cron daemon itself are considered to be the property of the system administrator.

The -s option of the "cronie" strain of cron daemon causes the job output to be also recorded to the syslog, to make it more easily available to the system administrator. It might also be useful on systems with no functioning local email service.

So, yes, you can redirect the output. But you should usually take care to redirect the standard error output too:

30 18 * * * rm /home/someuser/tmp/* > /home/t/mycron.log 2>/home/t/mycron.errors.log

...or if you want both standard output and standard error output in the same file, you might use this shorthand:

30 18 * * * rm /home/someuser/tmp/* > /home/t/mycron.log 2>&1

(You'll find that a standard rm command will generally output nothing at all to the standard output, and any errors will go into standard error output instead. That's why it's necessary to capture both types of output.)

  • Thanks. (1) Yes, I forgot to add 2>&1. (2) I mainly would like to know what those logging/email features provided by cron can do more than redirection in the command in the job, if we only care about the content of the output, not where the outut is written. (3) If I use redirection in the command of a job, will the output by the command not be captured by cron's logging or email features, correct? – Tim Nov 3 '18 at 14:56

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.