|
我写了一个perl脚本,手工执行没什么问题,执行结果也正确,但是当我通过cron来调用时,也执行了,执行也没什么错误,就是结果不对,程序如下,大概过程是通过ssh自动登陆到远程计算机,执行oracle数据备份操作,完成后,退出在通过sftp登陆上去下载该文件,通过cron调用后,执行了,但是没有备份文件也没有下载文件.
- #!/usr/bin/perl
- #
- # backup contract info data
- #
- # 2003-12-22
- #
- use Expect;
- sub getCurrentDate
- {
- $now = time();
- ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime($now);
- $mon++;
- $year+=1900;
- if($mday <1)
- {
- if($mon ==1 || $mon ==3 || $mon==5 || $mon==7||$mon==8||$mon==10||$mon==12)
- {
- $mday=30;
- if($mon==3 && $year%4==0)
- {
- $mday = 29;
- }
- elsif($mon ==3)
- {
- $mday =28;
- }
- }
- else
- {
- $mday = 31;
- }
- }
- $current_time = $year."-".$mon."-".$mday."-".$hour.":".$min.":".$sec;
- return $current_time;
- }
- $dbIP="192.168.0.21";
- $dbUserName="oracle";
- $dbPassword="seafox";
- $oracleParam="oauser/guandaoa2000";
- $backupPath="/home/oracle/backup";
- $currentDate=getCurrentDate;
- $fileName="OADB$currentDate.dmp";
- $backupFile="$backupPath/$fileName";
- my $timeout = 2;
- my $delay = 0;
- my $cmd = "ssh";
- my @params=("$dbIP","-l","$dbUserName");
- # backup file
- print("Begin backup file ....\n");
- $exp = Expect->spawn($cmd, @params) or die "Can't spawn $cmd\n"; # create an Expect object by spawning another process
- $exp->expect($timeout, -re=>'[Pp]assword:');
- $exp->send_slow($delay, "seafox\r\n");
- $exp->send("ls\r\n");# send some string there:
- $exp->send("exp $oracleParam file=$backupFile log=$backupPath/OAFileLog.log\r\n");
- $exp->send("exit\r\n");
- $exp->interact();
- $exp->hard_close();
- print("db exp finish.\n");
- #download file
- print("Begin download file ....\n");
- $cmd="sftp";
- $cmd="sftp $dbUserName\@$dbIP";
- $exp = Expect->spawn($cmd) or die "Can't spawn $cmd\n";
- $exp->expect($timeout, -re=>'[Pp]assword:');
- $an="yes";
- #$exp->send_slow($delay, "$an\r\n");
- $exp->send_slow($delay, "$dbPassword\r\n");
- $exp->send("ls\r\n");
- $exp->send("cd $backupPath\r\n");
- $exp->send("get $fileName\r\n");
- #$exp->send("get /home/oracle/OA.dmp\r\n");
- $exp->send("bye\r\n");
- $exp->interact();
- $exp->hard_close();
- print("backup finish !\n");
- exit 0;
复制代码 |
|