Cron job not running - postfix/sendmail error

19,327

According to cron manual:

  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)

If you want to stop mail alert, you should redirect standard output and standard error.

So you should modify your cron file as:

  @hourly /home/userName/ntpdate.sh >> /home/userName/ntpdateLog.txt 2>&1

and your script:

  echo "Current time is $(date), "
  ntpdate -u 192.168.1.25

Another way to do this is give an empty value to MAILTO variable at the top of your cron file:

  MAILTO=""
  @hourly /home/userName/ntpdate.sh >> /home/userName/ntpdateLog.txt 2>&1

I suggest you redirect std output and std error in any case because it is easier to debug the script in case of errors.

Here is crontab manual for more details.

Share:
19,327

Related videos on Youtube

trueCamelType
Author by

trueCamelType

I change stacks every couple of months, so I feel like a mid-level developer for life. I love learning new things, and happened to land in a career field that allows me to try lots of different things regularly. Trumpet player, Lumberjack, Hobbyist.

Updated on September 18, 2022

Comments

  • trueCamelType
    trueCamelType over 1 year

    Here is my content of sudo crontab -e

        @hourly /home/userName/ntpdate.sh
    

    The content of my script (ntpdate.sh) //.25 is my ntp server

         echo "Current time is $(date), " >> /home/userName/ntpdateLog.txt
         ntpdate -u 192.168.1.25 >> /home/userName/ntpdateLog.txt
    

    When I simply run the command by itself, or run the script by itself, it works fine and outputs to the file. The script has correct permissions to run.

    The error I'm getting in /var/log/syslog is this:

        CRON[6386]: (root) MAIL (mailed 1 byte of output; but got status 0x004b, #012)
        postfix/sendmail[6410]: fatal: open /etc/postfix/main.cf: No such file or directory
    

    Why is there a mail error when the script doesn't need mail (that I know of).

    I know that ntpdate is deprecated, but it is the only thing that does what I need right now (assuming I can get cron to run it).

    • Panther
      Panther almost 10 years
      cron runs in a minimal shell with minimal features and thus failure to use the full path is the most common problem. Although you included the full path in your script, within your script you do not. Use the full path to ntpdate and where you want ntpdateLog.txt to be written.
    • trueCamelType
      trueCamelType almost 10 years
      That makes sense, but it doesn't appear to be a problem. I'll change the script to reflect those changes
    • Martin Schröder
      Martin Schröder almost 10 years
      Set up ntpd and forget about ntpdate.