Cron如何影响Getopt :: Long模块?

Kav*_*rek 1 unix perl cron

我已经使用perl为mailx编写了一个包装器程序,它允许我轻松添加附件并做一些其他漂亮的事情,这些事情对于mailx来说有点令人沮丧.

在我的前几行中:

use strict;
use warnings;
use Getopt::Long;

my ( $to, $from, $subject, $attachments, $body, $file ) = (undef) x 7;

GetOptions(
    "to=s"          => \$to,
    "from=s"        => \$from,
    "subject=s"     => \$subject,
    "attachments=s" => \$attachments,
    "body=s"        => \$body,
    "file=s"        => \$file,
);
$to      = getlogin unless $to;
$from    = getlogin unless $from;
$subject = " "      unless $subject;
Run Code Online (Sandbox Code Playgroud)

到目前为止,这个包装器在被其他脚本调用时工作正常.但是现在我们有一个由Cron运行的脚本,一些有趣的事情正在发生.此Cron作业通过仅指定-t和-su来调用包装器,但省略-fr(正在使用标志的缩写).生成的电子邮件正确设置为:但是发件人列为-s@blah.com,主题行为空.根据上面的代码,我只能假设Cron和Getopt :: Long模块之间存在一些奇怪的问题.有谁知道为什么Cron的工作可能导致这种奇怪的行为?如果它是其他错误的东西会是什么?

sar*_*old 5

Perl getlogin可能不会从cron返回任何有用的内容,引用来自getlogin(3):

   getlogin() returns a pointer to a string containing
   the name of the user logged in on the controlling
   terminal of the process, or a null pointer if this
   information cannot be determined.
Run Code Online (Sandbox Code Playgroud)

我建议将crontab更改为始终为任何依赖的选项明确包含用户名getlogin.您还可以更改要使用的包装器getpwuid($<).(见perlvar(1)perlfunc(1)对细节$<getpwuid.)

为什么搞砸了你的mailx的,我不知道,但我要你使用反引号,猜测execsystem用绳子开始mailx的,而不是execsystem有一个列表.