-1

I have a cron task with "printf":

#!/bin/bash

# ..........
printf "hello\n"
# ..........

It runs once in N hours. However, in /var/log/syslog I don't see any logs with "hello" at all. Why not?

Jimku
  • 7
  • 1
  • 2
  • 2
    Try checking your mail box with the mail command. – Davidw Jun 23 '18 at 16:56
  • @Davidw what mail box? – Jimku Jun 23 '18 at 16:57
  • 2
    It depends on who is running the script. If it's you, then it should show up in your mail. (note that this assumes that sendmail is installed and configured for local system mail.) – Davidw Jun 23 '18 at 17:02
  • 1
    @Davidw what if it's not me and it's not installed? what does it have to do with email at all! – Jimku Jun 23 '18 at 17:15
  • 1
    https://superuser.com/questions/306163/what-is-the-you-have-new-mail-message-in-linux-unix – Davidw Jun 23 '18 at 18:09
  • 2
    Back up a second. Why do you think it should be in syslog? It shouldn't. Email as @Davidw is saying, is the default output. We have no idea how your system is up. What does any of this have to do with managing systems? You're looking for basics on how cron works. Serverfault isn't for learning the basics of Linux. –  Jun 23 '18 at 18:10
  • 1
    @Davidw I have nothing handling email in my system – Jimku Jun 23 '18 at 18:52
  • 1
    @yoonix ahahahaahahahaahahaha – Jimku Jun 23 '18 at 18:53
  • Install sendmail and configure it for local only. – Davidw Jun 23 '18 at 19:14
  • @Davidw I don't need to have it in sendmail – Jimku Jun 23 '18 at 23:58
  • Again, that is where it is. IT IS NOT IN the Syslog. – Davidw Jun 24 '18 at 21:32
  • Mail is how server stores messages printed by cron. It's what email was before it became what you know it to be. If you want your message to print in the syslog, you need to configure it to do that. Possible duplicate of https://serverfault.com/questions/137468/better-logging-for-cronjobs-send-cron-output-to-syslog – Christia Jun 25 '18 at 06:27
  • @Davidw again: I DON"T have senmail INSTALLEd – Jimku Jun 25 '18 at 12:48

1 Answers1

3

The designers of cron assumed that cron users would appropriately direct output (stdout & stderr) and, thus, any output that was not redirected to either a file or to another process was likely a mistake.

Their election was to forward via sendmail all spurious (i.e., un-redirected) output to the owner of the cron job.

If you check /var/log/syslog, you should find an entry like:

 Jun 26 22:18:01 sys0af3e3 CRON[16529]: (CRON) info (No MTA installed, discarding output)

The program typically called by cron is /usr/sbin/sendmail, but you can verify which is used in your implementation by running:

 strings /usr/sbin/cron | grep -i '^/.*mail'

If you were to create /usr/sbin/sendmail (it should be executable) with the contents:

 echo "####################" >> /tmp/mail.out 2>&1
 date >> /tmp/mail.out 2>&1
 cat >> /tmp/mail.out 2>&1

then run:

 systemctl restart cron  #this must be run as root

you would find your missing output appended to the file /tmp/mail.out after every run.

Don't forget to remove /usr/bin/sendmail and restart cron after you finish your test.

wineguy
  • 86
  • 4