Thread manchmal ist es NICHT einfach ! (21 answers)
Opened by cc at 2004-03-03 11:55

cc
 2004-03-03 11:55
#80519 #80519
User since
2004-01-09
55 Artikel
BenutzerIn
[default_avatar]
hallo

habe ernsthaftes problem, der ich selber nicht lösen kann:
bekomme 3 files: ein info file und 2 daten (TEXT) files
die files müssen zuerst gemäss info file umbenannt werden,
und per ftp geschickt werden: zuerst erstes und 15 minuten später das zweite file.
wobei wenn das senden von zweiten file nicht gelingt
(remote server down oder ähnliches), sollte 2 mal versucht werden.
diese zeitabstände sind sehr wichtig
und kein file darf 2 mal geschickt werden.
remote server hat nach 3 minuten timeout.
die files werden korrekt umbenannt, aber habe 2 probleme:

1.) wie kann ich ändern, dass zweite file zu einer anderer
remote directory (als der erste file) geschickt wird, z.B zu:
$ftp->cwd("DE/OUT");

2.) obwohl 2 files korrekt geschickt werden,bekomme per mail
nur die fehlermeldung: "files are NOT complete !"
eigentlich müsste ich dort auszug aus dem log über ftp transfer haben
wenn ich die sleep zeit zwischen den beiden ftp transfers
kürzer als timeout von ftp server einstelle,
dann wird der log per mail korrekt geliefert.

Code: (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/usr/bin/perl -w

use strict;
use warnings;
use File::Copy;
use Net::FTP;
use Net::Netrc;

# write the log
BEGIN
{
use CGI::Carp qw(carpout);
my $errorlog = "/var/ftp/log/errorlog.txt";
open(LOG, ">$errorlog") or die("Unable to open $errorlog: $!\n");
print LOG "Errors:\n";
carpout(*LOG);
}

my $recipient = "adress\@domain.de";
my $linux = "root\@domain.de";
my $server = "X.X.X.X";
my $user = "anonymous";
my $passwd = "";

chdir "/var/ftp" or die "/var/ftp: $!\n";

# DO NOT transfer without info file

-f "/var/ftp/info" or die "info file IS MISSING !\n";

# open and read info file
@ARGV = ("info");
my @ren;
while (<>) { # for every line in info
m/^\s*(\S+)\s+(\S+)\s*$/ or next; # That matches like "XXXXXXXX XXXXXXXX"
-f $1 or next; # skip if file to rename does not exist
-f $2 and next; # skip if file to rename does not exist
push @ren, [ $1, $2 ];
}

unless (@ren == 2) {
die "files are NOT complete !\n";
# extend to your feeling
}

# rename files according to info file
foreach my $f (@ren) {
my ($f1, $f2) = @$f;
move ($f1, $f2); # Rename files

# ftp transfer
my $ftp = Net::FTP->new ($server, Timeout => 9000, Debug => 3);
$ftp or die "$server: cannot connect: $@";
# If you don't use ~/.netrc
$ftp->login ($user,$passwd) or
die "$_: Could not login: " . $ftp->message;

# change remote directory for the first file
$ftp->cwd("/DE/IN");

# Put file 2 (not 1) to the ftp server
# server might go down, so retry twice
foreach my $try (0 .. 2) {
$ftp->put ($f2) and last; # Success
if ($try == 2) {

# send mail when transfer of second file failed
open(MAIL, "|/usr/sbin/sendmail -t");
print MAIL "from:$linux\n";
print MAIL "to:$recipient\n";
print MAIL "subject:ftp transfer of second file failed ! !\n";
print MAIL "hi \n\n";
print MAIL "ftp transfer of second file failed !\n\n";
print MAIL "Time: ", scalar localtime, "\n";
close(MAIL);

die "$server: cannot put $f2: " . $ftp->message;
}
print STDERR "Trasfer of $f2 failed: ",$ftp->message, ", retry in 10 minutes\n";
sleep (10 * 60);
}
$ftp->quit;

# And wait 15 minutes
sleep (15 * 60);
}

# read the log file
my $content;
open(FILE, "/var/ftp/log/errorlog.txt") || die "Cant open file. Reason: $!"; # (-:
while(<FILE>) {
$content .=$_; # get all of the file into content including past new lines
}
close(FILE);

# send the mail with log contents, when transfer completed
open(MAIL, "|/usr/sbin/sendmail -t") || die "Cant send mail. Reason: $!";
print MAIL "from:$linux\n";
print MAIL "to:$recipient\n";
print MAIL "subject:transfer was successfully !\n";
print MAIL "hi \n\n";
print MAIL "ftp transfer was successfully ! \n\n";
print MAIL "log file: \n";
print MAIL "$content \n\n";
print MAIL "Time: ", scalar localtime, "\n";
close(MAIL);

exit;
\n\n

<!--EDIT|cc|1078350984-->

View full thread manchmal ist es NICHT einfach !