crontab调度 perl含有mail发送的脚本,每次发信很多

Ldemon 2014-08-20 03:44:36
最近需要一个自动从文件服务器复制文件的脚本。
写了个 perl的 download的脚本,其中有 sendmail的子程序。

perl过程是,写一个配置文件(因为我的下载的东西路径每1个月一改的,不能再perl里写死),在其中指定挂在文件服务器的地址,账密,共享目录等等还有其他需要的信息。perl脚本读取这个文件然后mount这个共享,然后使用 system函数进行 mount和 cp。最后发邮件。
这个 perl脚本我手动执行非常好。然后我想用 crontab定时执行。

然后我的 crontab中我加了 flock锁防止copy时间过长产生多个实例。crontab的配置是:
[root@gvt01 ~]# crontab -l
20 14 * * 3 cd /home/Testing/ && flock /tmp/.download_lock -c "./buildDownload.pl buildDownload.ini > /home/Testing/sendmail.log 2>&1"

但是我为什么每次收到很多封邮件。而且奇怪的是:
每次我测试(比如改crontab里的时间为当前的10分钟后执行)运行一次这个脚本能收到一次邮件。但是第二次会收到2封,第三次3封,一次类推,折腾的我现在都能收 14封信了。哭。。。。
而且我在运行时一直 cat那个 sendmail.log文件,发现,在 sendmail子程序之前它都已经开始给我发信,我就收到信了。
从log里看,的确最后就发了一封信给我。

求高人解答!谢谢了
...全文
198 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ldemon 2014-08-22
  • 打赏
  • 举报
回复
还是没人解答吗? 求高手解决啊!
ljc007 2014-08-21
  • 打赏
  • 举报
回复
buildDownload.pl buildDownload.ini /home/Testing/sendmail.log 这三个文件的内容发出来看看
Ldemon 2014-08-21
  • 打赏
  • 举报
回复
版主大人,俺这写的可能有些幼稚或者啥的见谅。您先凑合看看给找出来原因。 我这个自己用 ./script script.ini > log.txt 2&1 很正常,也能发信。 我有点怀疑是不是我的postfix服务出问题了(我用的 RedHat 6.x,发信已经是 postfix服务了),可是发信啥的没问题啊,就是说次数多,然后开始我也说了用 flock在 crontab里边锁定执行了啊。 我是仅仅启动了 postfix然后别的啥都没配置,因为我的机器就在我们公司内网,播出去能找到邮件服务器直接发送到我的邮箱的。
Ldemon 2014-08-21
  • 打赏
  • 举报
回复
这是perl脚本

#!/usr/bin/perl -w
use strict;

my $os = $^O;
mkdir "/mnt/prod_build" unless( -e -d "/mnt/prod_build");
my %download = &Read_ini;#
my $user = $download{"User_Mail"};
my $group = $download{"Language_Group"};
my $ver = $download{"Version"};
my $server_ip = $download{"Server_IP"};
my $server_user = $download{"Server_User"};
my $server_passwd = $download{"Server_Passwd"};
my $server_path = $download{"Server_Share"};
my $script_path = $download{"Script_Path"};
my @strings = split /\//, $server_path;
shift @strings;
my $server_share = shift @strings;
my $path = join "/", @strings;
my @mail_body;
if($os =~ /linux/i){
my $build_server = "//$server_ip/$server_share";
my $id_pwd = "username=$server_user,password=$server_passwd";
my $build_mount = "mount -t cifs -o $id_pwd $build_server /mnt/prod_build/";
push @mail_body, "Mount the build server on Linux to /mnt/prod_build!\n" unless system $build_mount;
} elsif($os =~ /aix/i){
my $mount_command = "mount -v cifs -n $server_ip/$server_user/$server_passwd /$server_share /mnt/prod_build";
push @mail_body, "Mount the build server on AIX to /mnt/prod_build!\n" unless system $mount_command;
}
my $copy_path = "/mnt/prod_build/$path/$ver";
if(-e -d $copy_path){
push @mail_body, "Your mount build server for copy build success!\n";
chomp(my $build_number = `cat $copy_path/currentbuild.txt`);
print "$build_number!\n";
mkdir "$script_path/slp/dlp" unless( -e -d "$script_path/slp/dlp");
mkdir "$script_path/slp/xlp" unless( -e -d "$script_path/slp/xlp");
if(-e -d "$copy_path/dlp/$group/$build_number"){
push @mail_body, "Find the dlp build-number folder to copy!\n";
unless(system "\\cp -rf $copy_path/dlp/$group/$build_number $script_path/slp/dlp"){
push @mail_body, "Copy dlp build!\n";
if(-e -d "$script_path/slp/dlp/$build_number"){
unless(system "chmod -R +x $script_path/slp/dlp/$build_number"){
push @mail_body, "Add dlp execute access!\n";
}
}
}
}else{
push @mail_body, "Could Not find the dlp build to copy, dlp download FAILED!\n";
}
if(-e -d "$copy_path/xlp/$group/$build_number"){
push @mail_body, "Find the xlp build-number folder to copy!\n";
unless(system "\\cp -rf $copy_path/xlp/$group/$build_number $script_path/slp/xlp"){
push @mail_body, "Copy xlp build!\n";
if(-e -d "$script_path/slp/xlp/$build_number"){
unless(system "chmod -R +x $script_path/slp/xlp/$build_number"){
push @mail_body, "Add xlp execute access!\n";
}
}
}
}else{
push @mail_body, "Could Not find the xlp build to copy, xlp download FAILED!\n";
}
}
else{
push @mail_body, "Your mount build server to copy build, MOUNT FAILED!\n";
}
my $mail = join '#', @mail_body;
print "start sent mail action!\n";
&SendMail($mail,$user);

#Read the download build config information
sub Read_ini{
my %download;
while(<>){
chomp;
next if /\A\s*\Z/ || /\A\s*#/;
if(/=/){
my($key,$value) = /^\s*(\w+)\s*=\s*(\S*)\s*$/;
$download{$key} = $value;
}
}
return %download;
}

#Send mail to owner after build download completed
sub SendMail{
my ($body,$user) = @_;
my $os = $^O;
# $user =~ s/@/\\@/g; #AIX will be failed if add and parse @
my $title = "Your build download completed on <<$os>>!";
if($os =~ /linux/){
open MAIL, '|-', "mail -s \"$title\" $user";
print MAIL $body;
close MAIL;
} elsif($os =~ /aix/) {
# open MAIL, '|-', "uuencode $body script.log | mail -s \"$title\" $user";
#above line, because open file handle MAIL to "print MAIL @$body" add mail body failed on aix
# system "echo @$body | mail -s \"$title\" $user";
#above line, because echo in shell, it can not use directly in perl.
system "mail -s \"$title\" $user <<EOF
Auto download SLP build completed. There are 4 actions as blow:
$body
Auto download SLP build done.
EOF";
# print MAIL $body;
# close MAIL;
}
}


下边我的 properties文件

#Download build information
#User_mail should be your Company internal mail ID, if you want add more than one person, please use one blank betwee them.
#Example: use1@.companycom sue2@company.com use3@company.com
User_Mail=use1@company.com

#Language_group is the language group such as: G1 G2 etc, there should be base one your file server folder info.
Language_Group=G1

#Version, base on your file server which you copy build from.
Version=5.02

#Server_IP, your file server wihch you copy from
Server_IP=192.168.10.10

#Server_Share, your build path, this path until the Version folder.
#Example my build server is: //192.168.10.10/Prod/DAILYBUILD/NL_BUILD/SLP/5.02/dlp(xlp)/build1_20140101.
#then I set the value of Server_Share is: /Prod/DAILYBUILD/NL_BUILD/SLP/
Server_Share=/Prod/DAILYBUILD/NL_BUILD/SLP/

#Server_user, the user you used to login on
Server_User=tester

#Server_passwd, the user passwod if your file server
Server_Passwd=password

#Script_Path, the full path where this script and properties file load
Script_Path=/home/Testing/502

23,121

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧