【问题标题】:How does Cron affect the Getopt::Long module?Cron 如何影响 Getopt::Long 模块?
【发布时间】:2011-03-15 07:38:24
【问题描述】:

我使用 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;

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

【问题讨论】:

  • (undef) x 7 很聪明,但是 undef 是新的标量变量声明的默认值,所以这里不需要它。

标签: perl unix cron


【解决方案1】:

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.

我建议将您的 crontab 更改为始终明确包含依赖于 getlogin 的任何选项的用户名。您还可以更改包装器以使用getpwuid($<)。 (有关$<getpwuid 的详细信息,请参阅perlvar(1)perlfunc(1)。)

为什么会搞砸你的 mailx,我不知道,但我猜你使用反引号,execsystem 和字符串来启动 mailx,而不是 exec 或 @ 987654333@ 有一个列表。

【讨论】:

  • 这是我对 mailx 的开放: open( MAILX, "|$_mailx -t -r $from -s \"$subject\"" ) 我喜欢这些建议,但即使我得到了来自 getlogin() 的空指针不应该仍然正确设置主题吗?
  • perlfunc 建议成语:$login = getlogin || getpwuid($<) || "Kilroy";
  • @stocherilac,您的命令正在以mailx -t -r -s <subject> 的身份运行。 -s 开关似乎被解释为电子邮件地址的一部分,因为它紧跟在 -r 之后。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-03
  • 2019-07-20
  • 1970-01-01
相关资源
最近更新 更多