LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   cron job running but not getting the email that should send (https://www.linuxquestions.org/questions/linux-newbie-8/cron-job-running-but-not-getting-the-email-that-should-send-891769/)

linuxandtsm 07-14-2011 02:38 PM

cron job running but not getting the email that should send
 
Hi all,

i am using a crontab to run a perl script.
When i check the /var/log/cron, it shows that cron job ran successfully but i am not getting the output that the script should send. The same script runs successfully on a solaris server and sends output as email. Is there any other place to look for possible causes. Thanks in advance!

Code:

# crontab -l
00 07 * * * /usr/local/test/cron.d/report.pl > /dev/null 2>&1

#more /var/log/cron |grep report.pl
Jul 14 07:00:01 linux001 crond[21018]: (root) CMD (/usr/local/test/cron.d/report.pl > /dev/null 2>&1)

# ps -ef|grep cron
root      2675    1  0 Jan18 ?        00:00:05 crond
root      7475  6524  0 15:31 pts/0    00:00:00 grep cron
root    26588  2675  0 Jul13 ?        00:00:00 crond


brian-ocs 07-14-2011 03:15 PM

Quote:

00 07 * * * /usr/local/test/cron.d/report.pl > /dev/null 2>&1
Piping standard output and standard error to /dev/null probably isn't necessary, depending on what mechanism the script uses to generate the email message. You might try eliminating the "> /dev/null 2>&1" from the cron job and/or including this "silencing" function within the script itself.

It's hard to say without seeing the script, but my guess is that this is dumping the script's email output to /dev/null or some other critical output the script generates while executing.

It also might be an issue with your mail client. Does mail work for everything else?

linuxandtsm 07-14-2011 03:27 PM

Hi brian-ocs,

I am afraid to post the script as it is having 2986 lines.
I am hoping that the script should be good as it was working fine on another server.
Yes, the mail client working fine as i am getting other emails from other cronjobs.

chrism01 07-14-2011 05:53 PM

Well, redirect stdout & stderr to a file (instead of /dev/null) and see what you get in it. Also note that cron generally emails either the cron job owner or root if it has a problem.

Also, given this is Perl, have you tried
Code:

perl -wc report.pl
to check that it can find all the Perl modules referenced.
(That cmd does a Perl 'compile' & syntax check without executing the program. )

linuxandtsm 07-15-2011 08:32 AM

Thank you Very much chrism01,

owner of the cronjob is root.
Yes, redirecting output to a file showed that the script is executing successfully and giving the desired output.
The only problem is, it somehow not sending this output as an email.

BTW:
Code:

# perl -wc report.pl
report.pl syntax OK


catkin 07-15-2011 08:41 AM

Can you post the lines of the script that should be sending the mail and any associated "log" output?

lithos 07-15-2011 09:12 AM

This
Code:

..... > /dev/null 2>&1
prevents any output generated from cron to be sent to email.
Remove it and it should send an email with output.

I have this part of the code at the end of "ntpdate .... " line in cron when NTP syncs the time and I don't want to receive emails about executing it!

linuxandtsm 07-15-2011 09:23 AM

Hi catkin,

The following are some sections in the script related email.

Code:

our %output = (
              report_email => 1,                      # Send the report in email?
              report_stdout => 0,                    # Output it to stdout?
              report_txtsave => 1,                    # Save it in a text file?
              );



# If you set report_email above then change this section.
our %email = (
              recipients => 'myemail.mycompany.com', # <- Who gets the email report? 
           
              mailhost => '',                            # <- If we don't have sendmail installed, what
                                                        #  SMTP host should we use.  If you want to
                                                        #  use your system's sendmail set this to
                                                        #  ''.  Setting this option requires the
                                                        #  Net::SMTP module.
                          );


# Send the report in email.
#
sub email_report {
  my ( $self, $mailfrom, $mailto, $mailhost, $compress_attachments ) = @_;



# -> email
if( $output{report_email} ) {
  $R->attach_file( "actlog.txt", join("\n",@actlog_raw) ) if $email{attach_actlog};
  $R->attach_file( "serious_errrors.txt", join("\n",@errors) ) if $email{attach_data_errors};
  $R->attach_file( $tsm{drmdir}."/".$tsm{drmfile} )
    if( $email{attach_drm_plan} and  defined $tsm{drmfile} and -f  $tsm{drmdir}."/".$tsm{drmfile} );
  if( $email{attach_scripts} ) {
    my $S = "


$R->email_report( $email{sender}, $email{recipients}, $email{mailhost}, $email{compress_attachments} );

BTW what "log" should i be looking at ?

@lithos,

Yes i tried removing > /dev/null 2>&1 but no luck. Not getting emailed.

catkin 07-15-2011 10:20 AM

Quote:

Originally Posted by linuxandtsm (Post 4415693)
BTW what "log" should i be looking at ?

The "log" which was the file in "Yes, redirecting output to a file showed that the script is executing successfully and giving the desired output".

linuxandtsm 07-15-2011 10:54 AM

The output contains 1542 lines and it is all about the results of commands used in the script.
Is there any way to know what are the emails sent by the server like to whom,when,subject etc ?

catkin 07-16-2011 02:10 AM

Quote:

Originally Posted by linuxandtsm (Post 4415762)
The output contains 1542 lines and it is all about the results of commands used in the script.
Is there any way to know what are the emails sent by the server like to whom,when,subject etc ?

Not without some familiarity with the perl script's output/logging functionality or examining the code's flow, looking for parts that generate output and finding the corresponding lines in the output/log.

I'm no perl-spert, was just helping move the thread along by asking the right questions, but this looks wrong unless you have changed it to hide confidential information:
Code:

recipients => 'myemail.mycompany.com'

linuxandtsm 07-19-2011 09:39 AM

Code:

I'm no perl-spert, was just helping move the thread along by asking the right questions, but this looks wrong unless you have changed it to hide confidential information
Yes, i changed it to hide the info.

linuxandtsm 07-19-2011 12:51 PM

Hi all,

Now i know that redirecting the script's output to a file shows that script is running fine.
Now is there a way to modify cronjob so that it can send the output file to my email .
like
Code:

00 07 * * * /usr/local/test/cron.d/report.pl > /output.txt
...then what should be added to this cron job to send output.txt as email (not as an email attachment)

Thanks in advance!

Nermal 07-19-2011 05:13 PM

Code:

00 07 * * * /usr/local/test/cron.d/report.pl
this will e-mail root and if you have redirected the root mail to you it will arrive.

chrism01 07-19-2011 05:41 PM

If you just run it from the cmd line interactively, does it email you?
If yes, the its an env problem in cron; if no, its a general env /programming problem & you'll need to read the code/output to discover what.

Re cron env, try 'perl -wc report.pl' in cron; redirect the stdout+stderr to a file as previously advised.


All times are GMT -5. The time now is 01:13 PM.