LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to stop emails sent by daily cron job (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-stop-emails-sent-by-daily-cron-job-4175443883/)

sneakyimp 01-02-2013 06:49 PM

how to stop emails sent by daily cron job
 
I have a server that sends me two emails each day to report that a couple of cron jobs have completed. I'd like to prevent the email from arriving but am not sure why it is even sent. Both are sent from "Cron Daemon <root@example.com" and The first has this as Subject:
Code:

Cron <root@domU-AA-BB-CC-DD-EE-FF> test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
It contains this text:
Code:

/etc/cron.daily/webalizer:
+ /bin/touch /tmp/webalizer-cron-test
+ WEBALIZER=/usr/bin/webalizer
+ WEBALIZER_CONFDIR=/etc/webalizer
+ [ -x /usr/bin/webalizer ]
+ [ -d /etc/webalizer ]
+ awk $1 ~ /^LogFile$/ {print $2} /etc/webalizer/webalizer.conf
+ LOGFILE=/var/log/apache2/access.log.1
+ [ -s /var/log/apache2/access.log.1 ]
+ [ -r /var/log/apache2/access.log.1 ]
+ awk $1 ~ /^OutputDir$/ {print $2} /etc/webalizer/webalizer.conf
+ OUTDIR=/var/www/webalizer
+ [ /var/www/webalizer !=  ]
+ [ -d /var/www/webalizer ]
+ [ -w /var/www/webalizer ]
+ /usr/bin/webalizer -c /etc/webalizer/webalizer.conf -Q
4 records (4 ignored) in 0.00 seconds
+ RET=0
+ awk $1 ~ /^LogFile$/ {gsub(/\.[0-9]+(\.gz)?/,""); print $2} /etc/webalizer/webalizer.conf
+ NLOGFILE=/var/log/apache2/access.log
+ [ /var/log/apache2/access.log.1 != /var/log/apache2/access.log ]
+ [ -s /var/log/apache2/access.log ]
+ [ -r /var/log/apache2/access.log ]
+ /usr/bin/webalizer -c /etc/webalizer/webalizer.conf -Q /var/log/apache2/access.log
4 records (2 ignored) in 0.50 seconds
+ RET=0
+ exit 0

The second email has this subject:
Code:

Cron <root@domU-AA-BB-CC-DD-EE-FF> php /var/www/cron/notify_expired_licenses.php > /var/www/cron/notify_expired_licenses.log
and it contains some harmless errors:
Code:

PHP Notice:  Undefined index: QUERY_STRING in /var/www/html/includes/foo.php on line 24
PHP Notice:  Undefined index: HTTP_USER_AGENT in /var/www/html/includes/foo.php on line 29

Can anyone tell me why these emails are being sent and how I might stop them?

chrism01 01-02-2013 07:07 PM

If a cron'ed program produces output that is not re-directed to a specified logfile (for stdout & stderr), then the output will be received by cron and emailed to the job owner (normally; if cron has an error it may email root instead).
I'd say you need to look at the scripts it's running and ensure the output is re-directed to a named log.

BTW, the first one resembles the output generated by using the 'set' cmd in a shell script, usually used for debugging only.

Eg, if you've got a shell script that is going wrong and you can't figure out why from the normal output, try this
Code:

#!/bin/bash
set -xv

The set cmd shows what the parser is doing, in detail.

sneakyimp 01-02-2013 08:12 PM

Thanks for your response. I've checked every script in the chain of execution and didn't find any echo statements or set commands link you described, but it would appear that the webalizer script has a "Quiet" setting in /etc/webalizer/webalizer.conf which is currently set to "no":
Code:

# The Quiet option suppresses output messages... Useful when run
# as a cron job to prevent bogus e-mails.  Values can be either
# "yes" or "no".  Default is "no".  Note: this does not suppress
# warnings and errors (which are printed to stderr).

Quiet          no

I'll try setting that to "yes" and see if it helps.

As for the other script, I should be able to modify the PHP script to get rid of the E_NOTICE errors. I've I have any trouble, I'll be checking back in.

shivaa 01-02-2013 10:55 PM

Quote:

I've checked every script in the chain of execution and didn't find any echo statements or set commands link you described...
Perhaps set cmd isn't mentioned in your script, but options like -xv (for debugging script) can be there in form of:-
Code:

#!/bin/bash -xv
If it is, then remove -xv option from the script.

Also what are crontab entries for both these jobs. Such mails can be avoided by changing the '<command>' part in crontab.

sneakyimp 01-02-2013 11:23 PM

Hmmm. Thanks, Shivaa. Now that I look closer at /etc/cron.daily/webalizer, I see that the shebang has a -x flag. Do you think that could be it?
Code:

$ cat /etc/cron.daily/webalizer
#!/bin/sh -x
# a test because cron was not finishing
#/bin/touch /tmp/webalizer-cron-test-by-jaith
# /etc/cron.daily/webalizer: Webalizer daily maintenance script
# This script was originally written by
# Remco van de Meent <xxx@example.com>
# and now, all rewrited by Jose Carlos Medeiros <yyy@example.com>

# This script just run webalizer agains all .conf files in /etc/webalizer directory

WEBALIZER=/usr/bin/webalizer
WEBALIZER_CONFDIR=/etc/webalizer

[ -x ${WEBALIZER} ] || exit 0;
[ -d ${WEBALIZER_CONFDIR} ] || exit 0;

for i in ${WEBALIZER_CONFDIR}/*.conf; do
  # run agains a rotated or normal logfile
  LOGFILE=`awk '$1 ~ /^LogFile$/ {print $2}' $i`;

  # empty ?
  [ -s "${LOGFILE}" ] || continue;
  # readable ?
  [ -r "${LOGFILE}" ] || continue;
 
  # there was a output ?
  OUTDIR=`awk '$1 ~ /^OutputDir$/ {print $2}' $i`;
  #  exists something ?
  [ "${OUTDIR}" != "" ] || continue;
  # its a directory ?
  [ -d ${OUTDIR} ] || continue;
  # its writable ?
  [ -w ${OUTDIR} ] || continue;

  # Run Really quietly, exit with status code if !0
  ${WEBALIZER} -c ${i} -Q || continue;
  RET=$?;

  # Non rotated log file
  NLOGFILE=`awk '$1 ~ /^LogFile$/ {gsub(/\.[0-9]+(\.gz)?/,""); print $2}' $i`;

  # check current log, if last log is a rotated logfile
  if [ "${LOGFILE}" != "${NLOGFILE}" ]; then
    # empty ?
    [ -s "${NLOGFILE}" ] || continue;
    # readable ?
    [ -r "${NLOGFILE}" ] || continue;

    ${WEBALIZER} -c ${i} -Q ${NLOGFILE};
    RET=$?;
  fi;
done;

# exit with webalizer's exit code
exit $RET;


shivaa 01-02-2013 11:48 PM

Quote:

I see that the shebang has a -x flag. Do you think that could be it?
Yes, it is. Just remove -x from shebang. (Note: -x option stands for execution and -v for verbose).
Could you share crontab enties?

sneakyimp 01-03-2013 12:24 AM

This is in /etc/crontab:
Code:

$ cat /etc/crontab
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user        command
17 *        * * *        root    cd / && run-parts --report /etc/cron.hourly
25 6        * * *        root        test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6        * * 7        root        test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6        1 * *        root        test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#

This is what I get from crontab -e which runs the second script:
Code:

# m h  dom mon dow  command
0 4 * * * php /var/www/cron/notify_expired_licenses.php > /var/www/cron/notify_expired_licenses.log


theFreethinker 01-03-2013 01:19 AM

Quote:

Originally Posted by sneakyimp (Post 4861799)
This is in /etc/crontab:
Code:

$ cat /etc/crontab
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user        command
17 *        * * *        root    cd / && run-parts --report /etc/cron.hourly
25 6        * * *        root        test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6        * * 7        root        test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6        1 * *        root        test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#


As there are no output redirections all output is sent in email to the starter of the cronjob, i.e. root in this case. And the '+' signs in your inital post indicate that most of the output comes from the shell debugger. As mentioned earlier, remove the 'x' and 'v' flags from the scripts. In crontab redirect STDOUT to /dev/null for example and do not redirect STDERR, so you still catch the errors if they occur.

Quote:

Originally Posted by sneakyimp (Post 4861799)
This is what I get from crontab -e which runs the second script:
Code:

# m h  dom mon dow  command
0 4 * * * php /var/www/cron/notify_expired_licenses.php > /var/www/cron/notify_expired_licenses.log


There is output redirection, but because script produces errors and STDERR is not redirected (as it is correct in most crontab cases) you get the email notification. Fix the script (or get rid of the problem) and and you wont get any annoying emails.

sneakyimp 01-03-2013 12:52 PM

I believe I've fixed the PHP script so it shouldn't pose any more trouble I don't think.

I removed the -x flag on the webalizer script and it still insists on outputting a summary of records processed, but I think I can alter that quiet setting and I think that'll fix it.

Thanks for the help! I'll know by tomorrow if I have it fixed.


All times are GMT -5. The time now is 09:22 PM.