“mail:sender”不是从cron工作,而是在手动运行时工作

时间:2015-03-19 10:13:41

标签: perl shell unix crontab mail-sender

我有一个shell脚本,它将调用perl脚本。 perl脚本具有邮件发送功能。当我从命令提示符手动运行并且还发送邮件时,脚本运行得非常好,而当从crontab调度时,shell和perl都按照日志运行,但邮件没有交付。

请在下面找到代码段


Shell脚本: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脚本: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条目

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

请帮帮我

2 个答案:

答案 0 :(得分:0)

在cron中试试这个:

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

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

答案 1 :(得分:0)

问题在于环境变量,我们必须确保在手动运行时也可以从crontab中获得所有环境变量。我已按照以下步骤实现

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