Perl 脚本根本不在 Crontab 中运行
我正在尝试从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来我运行它的目录很重要。当我像这样从 root 运行它时:
我得到了一组与此完全不同的结果:
考虑到这一点,我将其添加到脚本中:
它最终从 cron 运行。即使我在第一个场景中指定了路径,仍然不明白为什么结果不同。
谢谢大家的回答。我的答案甚至不是真正的答案,只是一个解决方法
It appears that the directory that I run it from matters. When I run it from root like this:
I get a completely different set of results to this:
With that in mind, I added this to the script:
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
一些提示:
不要忘记警告:
更好地使用词法 DIR 句柄:
你确实意识到 '.'和“..”现在在您的列表中,是吗?
除非您当前的工作目录是 $TG_Stats_directory!否则这会失败!:
您不是说文件以 .csv 结尾吗?
哈特哈,
保罗
Some pointers:
don't forget warnings:
better use lexical DIR-handles:
You do realize that '.' and '..' are in your list now, do you?
This fails UNLESS your current working directory IS $TG_Stats_directory!:
don't you mean files ENDING in .csv?
HTH,
Paul