0

我有一个 shell 脚本,它会反过来调用一个 perl 脚本。perl 脚本具有邮件发送功能。当我从命令提示符手动运行脚本并传递邮件时,脚本运行得非常好,而当从 crontab 调度时,shell 和 perl 都根据日志运行,但邮件没有得到传递。

请在下面找到代码片段


外壳脚本: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

外壳脚本: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

请帮我

4

2 回答 2

0

在 cron 中试试这个:

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

或者

 * * * * * /usr/bin/sh /home/pm_prod/PM/bin/ALERT/rmail.sh
于 2015-03-19T10:24:59.007 回答
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

拉古

于 2015-03-23T05:20:56.653 回答