Perl 脚本根本不在 Crontab 中运行

发布于 2024-12-17 16:36:46 字数 927 浏览 0 评论 0原文

我正在尝试从 crontab 运行 perl 脚本,但遇到困难。当我从命令行运行它时它工作得很好。我已经搜索并看到了有关环境等并指定完整路径的各种答案,但我不确定我是否做得正确。下面是脚本

#!/usr/bin/perl

$ENV{'PATH'} = '/usr/bin:/usr/sbin/usr/local/bin';

use strict;

my $TG_Stats_directory = "/export/home/SonusNFS/TG_Stats";

opendir TG_Stats, $TG_Stats_directory;

my @GSX_directories = readdir TG_Stats;

foreach my $subdir (@GSX_directories) {

        opendir GSX, $subdir;
        my @csv_listing = readdir GSX;
                foreach my $file (@csv_listing){
                        if ($file =~ /\.csv/){
                                unlink $TG_Stats_directory."/".$subdir."/".$file;
                        }               
                }
        close GSX;      

}       

close TG_Stats;

下面是 crontab 条目。

25 03 * * * /usr/bin/perl /export/home/SonusNFS/TG_Stats/rmdir.pl 2>/tmp/cronerrors.txt

我缺少什么?我需要添加什么才能让它在 Cron 中工作?

I'm trying to run a perl script from crontab but I'm having difficulty. It works perfectly when I run it from the command line. I've searched and seen various answers about Environment etc and specifying full paths but I'm not sure if I'm doing it correctly. Below is the script

#!/usr/bin/perl

$ENV{'PATH'} = '/usr/bin:/usr/sbin/usr/local/bin';

use strict;

my $TG_Stats_directory = "/export/home/SonusNFS/TG_Stats";

opendir TG_Stats, $TG_Stats_directory;

my @GSX_directories = readdir TG_Stats;

foreach my $subdir (@GSX_directories) {

        opendir GSX, $subdir;
        my @csv_listing = readdir GSX;
                foreach my $file (@csv_listing){
                        if ($file =~ /\.csv/){
                                unlink $TG_Stats_directory."/".$subdir."/".$file;
                        }               
                }
        close GSX;      

}       

close TG_Stats;

Below is the crontab entry.

25 03 * * * /usr/bin/perl /export/home/SonusNFS/TG_Stats/rmdir.pl 2>/tmp/cronerrors.txt

What am I missing? What do I need to add to get it working in Cron?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

撩心不撩汉 2024-12-24 16:36:46

看来我运行它的目录很重要。当我像这样从 root 运行它时:

# cd /
# perl /export/home/SonusNFS/TG_Stats/rmdir.pl

我得到了一组与此完全不同的结果:

# cd /export/home/SonusNFS/TG_Stats/
# perl rmdir.pl

考虑到这一点,我将其添加到脚本中:

my $directory = "/export/home/SonusNFS/TG_Stats";
chdir($directory) or die "Can't chdir to $directory $!";

它最终从 cron 运行。即使我在第一个场景中指定了路径,仍然不明白为什么结果不同。

谢谢大家的回答。我的答案甚至不是真正的答案,只是一个解决方法

It appears that the directory that I run it from matters. When I run it from root like this:

# cd /
# perl /export/home/SonusNFS/TG_Stats/rmdir.pl

I get a completely different set of results to this:

# cd /export/home/SonusNFS/TG_Stats/
# perl rmdir.pl

With that in mind, I added this to the script:

my $directory = "/export/home/SonusNFS/TG_Stats";
chdir($directory) or die "Can't chdir to $directory $!";

It finally ran from cron. Still don't understand why the results were different even though I specified the path in the first scenario.

Thanks for all the answers though guys. My answer isn't even a real answer, just a workaround

不忘初心 2024-12-24 16:36:46

一些提示:

> $ENV{'PATH'} = '/usr/bin:/usr/sbin/usr/local/bin';

$ENV{PATH} = '/usr/bin:/usr/sbin:/usr/local/bin';

> use strict;

不要忘记警告:

use warnings;

> my $TG_Stats_directory = "/export/home/SonusNFS/TG_Stats";

> opendir TG_Stats, $TG_Stats_directory;

更好地使用词法 DIR 句柄:

opendir my $TG_Stats... or die $!;

> my @GSX_directories = readdir TG_Stats;

你确实意识到 '.'和“..”现在在您的列表中,是吗?

my @GSX_directories = grep !/^\.\.?$/, readdir TG_Stats;

> foreach my $subdir (@GSX_directories) {

>     opendir GSX, $subdir;

除非您当前的工作目录是 $TG_Stats_directory!否则这会失败!:

opendir my $GSX, "$TG_Stats_directory/$subdir" or die $!;

>     my @csv_listing = readdir GSX;

>             foreach my $file (@csv_listing){
>                     if ($file =~ /\.csv/){

您不是说文件以 .csv 结尾吗?

                      if ( $file =~ /\.csv$/ ) {

>                             unlink $TG_Stats_directory."/".$subdir."/".$file;
>                     }               
            }
>     close GSX;      

      closedir $GSX;
> }       

> close TG_Stats;

closedir $TG_Stats;

哈特哈,
保罗

Some pointers:

> $ENV{'PATH'} = '/usr/bin:/usr/sbin/usr/local/bin';

$ENV{PATH} = '/usr/bin:/usr/sbin:/usr/local/bin';

> use strict;

don't forget warnings:

use warnings;

> my $TG_Stats_directory = "/export/home/SonusNFS/TG_Stats";

> opendir TG_Stats, $TG_Stats_directory;

better use lexical DIR-handles:

opendir my $TG_Stats... or die $!;

> my @GSX_directories = readdir TG_Stats;

You do realize that '.' and '..' are in your list now, do you?

my @GSX_directories = grep !/^\.\.?$/, readdir TG_Stats;

> foreach my $subdir (@GSX_directories) {

>     opendir GSX, $subdir;

This fails UNLESS your current working directory IS $TG_Stats_directory!:

opendir my $GSX, "$TG_Stats_directory/$subdir" or die $!;

>     my @csv_listing = readdir GSX;

>             foreach my $file (@csv_listing){
>                     if ($file =~ /\.csv/){

don't you mean files ENDING in .csv?

                      if ( $file =~ /\.csv$/ ) {

>                             unlink $TG_Stats_directory."/".$subdir."/".$file;
>                     }               
            }
>     close GSX;      

      closedir $GSX;
> }       

> close TG_Stats;

closedir $TG_Stats;

HTH,
Paul

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文