0

I have a shell script which will in-turn invokes a perl script. THe perl script has a mail sending functionality. The script runs very well when i run it manually from command prompt and delivers the mail also, while when scheduled from crontab the shell and perl both runs as per the log, but mail is not getting delivered.

Please find below code snippet


Shell Script :rmail.sh

#!/bin/sh

. /home/pm_prod/.bash_profile
export PATH=$PATH:/home/orapps/client/oracle/product/10.2.0/client_1/bin:/usr/kerberos/bin:/data2/software/oracle/product/10.2.0/client_1/bin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/pm_prod/bin/:

perl  /home/pm_prod/PM/bin/ALERT/rmail.pl

Shell Script :rmail.pl

#!/usr/bin/perl -w
use strict;
use Mail::Sender;

# Send the file.  Change all variables to suit
my $sender = new Mail::Sender
{
    smtp => 'some.smtpserver.com',
    from => 'somename@somedomain.com'
};

$sender->MailFile(
{
    to      => 'somename@somedomain.com',
    subject => 'File',
    msg     => "Here is the data.\n",
    file    => '/home/pm_prod/PM/bin/ALERT/attachement_file.txt',
});

CronTab Entry

* * * * * sh /home/pm_prod/PM/bin/ALERT/rmail.sh

Please help me

2 Answers2

0

Try this in cron:

* * * * * /bin/sh /home/pm_prod/PM/bin/ALERT/rmail.sh

Or

 * * * * * /usr/bin/sh /home/pm_prod/PM/bin/ALERT/rmail.sh
Sergio Teixeira
  • 72
  • 1
  • 1
  • 7
0

The issue is with the environment variables, we have to make sure to have all the environment variable when run manually be applicable from the crontab as well. I have followed below steps to achieve that

1. Get the current ENV variables from normal user and put them in to a file
env > /home/pm_prod/workspace/pmenv

2. Copy the content of pmenv file to my rmail.sh script

3. Now schedule rmail.sh script in crontab.

    Note : If you are too tired to test the script in crontab, you an optionally try to create a cron type environment with below command and test them before actually scheduling them as mentioned in point 3
* * * * * env > /home/pm_prod/workspace/cronenv
env - `cat /home/pm_prod/workspace/cronenv` /bin/sh

Raghu