免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2612 | 回复: 5

[SCO UNIX] crontab cannot execute a perl program(*.pl) THANK YOU [复制链接]

论坛徽章:
0
发表于 2004-09-14 16:25 |显示全部楼层
Dear ALL,
here is my crontab content by user "root"
4 4-23 * * * cd /usr/gm/lsmscript;/usr/gm/lsmscript/lsm.pl -section frequent >;/dev/null 2 >;/dev/null.

lsm.pl
#!/usr/bin/perl

require "readtxt.pl";
require "util.pl";
require "initfieldinfo.pl";

my($i, $sDestination, $sSrcFile, $tmp, $sTmpDir, $sTmpArc, $sTmpStr, $iNumArgv, $sErr, $iNewErr);

$iNewErr = 1;
############################################################################
# To check whether the LSM is already running

if (open(LOG, "</tmp/_lsmmode") {
        close (LOG);
        print "LSM is already running\n";
#        MessLog ($sLogFile, "LSM is already running\n";
        exit (1);
}


############################################################################
# The setup file
my $gInFile = "./lsmsetup.inf";


############################################################################
# Read the setup section

@section_info = &OpenInf($gInFile, "setup";
foreach $tmp (@section_info)
{
        if ($tmp =~ /^(.*)=(.*)/)
        {
                #sTmpDir is used to store the tmp destination
                #where the file is copy to the LSM.
                if ($1 eq "tempdir" {
                        $sTmpDir = $2;                       
                        `chmod -R 777 $sTmpDir`;
                        `rm -R $sTmpDir*`;
                }
                elsif ($1 eq "logfile" {
                        $sLogFile = $2;
                }
        }
}


############################################################################
# Check whether the log file can be opened

if (not open (LOG, ">;>;$sLogFile") {
        print "\nCannot open the log file $sLogFile\n";
        exit 1;
}
else {
        close(LOG);
}

############################################################################
# To check whether the parameters input is correct

$iNumArgv = @ARGV;
if ($iNumArgv != 2) {
        usageError();
}

if ($ARGV[0] eq "-section" {
        @section_info = &OpenInf($gInFile, $ARGV[1]);
        $i = @section_info;
        if ($i eq "0" {
                print "Invalid time section has been input\n";
                MessLog ($sLogFile, "Invalid time section has been input\n", $iNewErr);
                $iNewErr = 0;
                exit 1;
        }
}
else {
        usageError();
        Messlog ($sLogFile, "Invalid time section has been input\n";
        $iNewErr = 0;
        exit 1;
}

############################################################################
# Read the configuration

#@section_info = &OpenInf($gInFile, "files";

foreach $tmp (@section_info)
{
        $tmp =~ tr/ //d;
        chomp $tmp;
        if ($tmp =~ /^(.*)=(.*)/)
        {
               
                #$1 store each header of the configuration
                #which is used to define the tmp dir of each
                #configuration
                $i = $1;

                #$2 store the configuration
                $sTmpStr = $2;
                $sTmpStr =~ /^(.*),(.*),(.*),(.*)/g;

                #$2 stores the destination path
                #$3 stores the owner of the file
                #$4 stores the mode of the file
                $Src{$i} = $1;
                $Dest{$i} = $2;
                $Owner{$i} = $3;
                $Mode{$i} = $4;

                #tmp destination file for the zip arch
                $sTmpArc = "$sTmpDir"."$i.tar";

                # $1 here is the source file directory
                # lsmzip.s is a script that go to the $1 directory and then zip the file
                # This script has nothing to print out if its action success.
                print "$i\n";
print "Src: $Src{$i} sTMpARc: $sTmpArc\n";
                if ($i =~ /^dir*/) {
                        $sErr = `cd $Src{$i}; tar cvf $sTmpArc *`;

                        if ($sErr eq "") {
                                print "Error occur 6\n";
                                MessLog($sLogFile, "There is error when processing tar cvf $sTmpArc *\n", $iNewErr);
                                $iNewErr = 0;       
                                #remove all the file in the tmp file
#                                `chmod -R 777 $sTmpDir`;
#                                `rm -R $sTmpDir/*`;
#                                exit 1;
                        }
                }
#                elsif ($i =~ /^file*/) {
#                        print "file wo\n";
#                        $sErr = `tar cvf $sTmpArc $1`;       
#                }

        }
}


###########################################################################
# unzip the zip file in the temp destination to the real destination

foreach $tmp (sort keys %Dest)
{

        #Extract the tar file to a tmp directory

        if ($tmp =~ /^dir*/) {
                $sTmpArc = "$sTmpDir"."$tmp".".tar";
                $sErr = `tar xvf $sTmpArc -C $Dest{$tmp}`;

                if ($sErr eq "") {
                        print "Error occur 1\n";
                        MessLog ($sLogFile, "There is error when extracting $sTmpArc to $Dest{$tmp}\n", $iNewErr);       
                        $iNewErr = 0;       
                }

                #Change the owner of the files
                $sErr = `chown -R $Owner{$tmp} $Dest{$tmp} 2>;&1`;
                if (!($sErr eq "")) {
                        print "Error occur 2\n";
                        MessLog($sLogFile, "$sErr\n", $iNewErr);
                        $iNewErr = 0;       
                }
       
                #Change the mode of the files
                $sErr = `chmod -R $Mode{$tmp} $Dest{$tmp} 2>;&1`;
                if (!($sErr eq "")) {
                        print "Error occur 3\n";
                        MessLog($sLogFile, "$sErr\n", $iNewErr);
                        $iNewErr = 0;       
                }
        }
        elsif ($tmp =~ /^file*/) {
                print "cp $Src{$tmp} $Dest{$tmp} 2>;&1 \n";
                $sErr = `cp -f $Src{$tmp} $Dest{$tmp} 2>;&1`;

                if (!($sErr eq "")) {
                        print "Error occur 4\n";
                        MessLog ($sLogFile, "$sErr\n", $iNewErr);
                        $iNewErr = 0;
                }

                #Change the owner of the files
                $sErr = `chown $Owner{$tmp} $Dest{$tmp} 2>;&1`;
                if (!($sErr eq "")) {
                        print "Error occur 5\n";
                        MessLog($sLogFile, "$sErr\n", $iNewErr);
                        $iNewErr = 0;       
                }
       
                #Change the mode of the files
                $sErr = `chmod $Mode{$tmp} $Dest{$tmp} 2>;&1`;
                if (!($sErr eq "")) {
                        print "Error occur 6\n";
                        MessLog($sLogFile, "$sErr\n", $iNewErr);
                        $iNewErr = 0;       
                }
        }
        else {
                        MessLog ($sLogFile, "Invalid configuration of the setup file.\n", $iNewErr);
        }


}

#Delete all temp files.
`chmod -R 777 $sTmpDir`;
`rm -R $sTmpDir*`;


print "LSM finishes\n";

sub MessLog
{
        my ($fFile, $sReturn, $iNew)=@_;
        my $data=shift;
        my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdat) = localtime(time);
        my $dd = "";
        my $ti = "";

        $year = substr($year,1,2);
        $mon = $mon + 1;
        $mon = sprintf("%02d",$mon);
        $mday = sprintf("%02d", $mday);

        $dd = $year."/".$mon."/".$mday;

        $sec = sprintf("%02d", $sec);
        $min = sprintf("%02d", $min);
        $hour = sprintf("%02d", $hour);
        $ti = $hour.":".$min.":".$sec;

        if (open(LOG, ">;>;$fFile"))
        {
                if ($iNew eq "1") {
                        print LOG "**** [$dd - $ti]:";
                        print LOG "\n";
                }
                print LOG "$sReturn\n";
                print LOG "\n";
                close LOG;
        }

        else
        {
                print "The file $tFile cannot be opened\n";
        }
}

sub usageError() {
        print "\nSyntax error\n\n";
        print "Usage: perl lsm.pl -section [time]\n\n";
        exit(1);
}

lsmsetup.inf

[frequent]
dir1=/mnt/2nd/u/z01/daily/,/u/z01/daily/,z01:group,755
dir2=/mnt/2nd/u/z01/tty/,/u/z01/tty/,z01:group,755

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
发表于 2004-09-14 19:26 |显示全部楼层

crontab cannot execute a perl program(*.pl) THANK YOU

>;/dev/null 2 >;/dev/null
把输出转到文件看一下
应该是权限,或者环境变量的问题造成的

cd /usr/gm/lsmscript;/usr/gm/lsmscript/lsm.pl
crontab 一行调用2个语句,是否能行我没试过
把cd 写到你的shell前面不就行了

论坛徽章:
0
发表于 2004-09-14 20:07 |显示全部楼层

crontab cannot execute a perl program(*.pl) THANK YOU

firstly THANKS for your response.
we have tested as your proposal,
but no error display. and multi commmands can be execute by";".
we think that which file can define the cron's default environment, such as PATH, etc.
Could you tell us it?
because the script is perl's script.


THANK YOU VERY MUCH!!

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
发表于 2004-09-14 20:26 |显示全部楼层

crontab cannot execute a perl program(*.pl) THANK YOU

没有错误信息可以加些调试信息,看下是shell没执行,还是执行到某一步报错

在命令行执行env 看下需要什么变量
perl的偶不熟悉
或者在shell前面加上
. $HOME/.profile
HOME根据实际路径填写

还有你是用crontab -e 加入的命令,还是直接vi 打开文件的?
如果是后者需要重新启动cron进程

论坛徽章:
0
发表于 2004-09-14 22:20 |显示全部楼层

crontab cannot execute a perl program(*.pl) THANK YOU

we use "crontab -e " to append a line in the file.
and cron daemon running normally.
by the way, the script running in shell is OK.
the key is that PATH environment is different on shell and cron daemon.

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
发表于 2004-09-14 22:24 |显示全部楼层

crontab cannot execute a perl program(*.pl) THANK YOU

cron 不会取 .profile 中的设置的,需要在shell自行定义的
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP