I have a script running on a utility server that works flawlessly when run interactively on a Windows 2000 machine. If I double-click on it, or run it at the command line everything is fine. When I schedule it, only part of the script seems to function. Here's the detail:
The machine has a share from a remote machine mapped to a local drive. The share contains a bunch of IIS logs. The script is to go into that drive, recurse through all the directories, zip up all the log files, delete the originals if successful, and then email me the results of its actions. Like I said, when I double click the script it works fine.
I'm using the command-line interface to WinZip in the following format to a) kick off the zipping and to store the results of it in $result.
Here's the script:
###########################################################
# IIS Log File Archiver
# Resurses through directories and zips all files,
# removes the old file if successful, then emails a
# detailed log file to the specified email address.
###########################################################
$directory = 'K:\';
$zipApp = 'D:\IISArchive\wzzip.exe';
$logFile = '';
$SMTPServer = '10.0.1.2';
$mailTo = 'me@me.com';
$reportSubject = 'Daily IISArchive Report';
###########################################################
use Socket;
$|++;
&recurseDirectory($directory);
&emailLogFile();
exit;
###########################################################
sub recurseDirectory {
my $zipApp = 'D:\IISArchive\wzzip.exe';
my $thisDirectory = $_[0] . "\\";
opendir (THISDIR, $thisDirectory);
my @theseContents = readdir (THISDIR);
closedir (THISDIR);
foreach my $item (@theseContents) {
if (!($item =~ /\.zip$/)) {
if (!($item =~ /^\./)) {
my $temp = $thisDirectory.$item;
if (-d $temp) {
$::logFile .= "\n_____________________\n$item\
+n";
&recurseDirectory($temp);
} else {
my $tempZipped = $temp;
$tempZipped =~ s/\.log/\.zip/g;
if (¬TodaysLogFile($item)) {
$::logFile .= "$::zipApp \"$tempZipped\" \
+"$temp\"\n";
my $result = `$::zipApp -m \"$tempZipped\"
+ \"$temp\"\n`;
$::logFile .= $result."\n";
if ($result =~ /Unable to erase the file:/
+) {
unlink($tempZipped);
}
} else {
$::logFile .= " --> Skipping today's log f
+ile, $item\n";
}
}
}
}
}
}
###########################################################
sub notTodaysLogFile {
my @times = localtime(time);
$times[5] = $times[5] - 100;
if ($times[5] < 10) {
$times[5] = "0".$times[5];
}
$times[4]++;
if ($times[4] < 10) {
$times[4] = "0".$times[4];
}
if ($times[3] < 10) {
$times[3] = "0".$times[3];
}
if ($_[0] =~ /$times[5]$times[4]$times[3]\.log/) {
return 0;
} else {
return 1;
}
}
###########################################################
sub emailLogFile {
my $smtp_server = $::SMTPServer;
my $pop_server = $smtp_server;
my $to = $::mailTo;
my $subject = $::reportSubject;
my $message = $::logFile;
my $proto = getprotobyname('tcp');
socket(SOCK, AF_INET, SOCK_STREAM, $proto);
my $iaddr = gethostbyname($smtp_server);
my $port = getservbyname('smtp', 'tcp');
my $sin = sockaddr_in($port, $iaddr);
connect(SOCK, $sin);
my $err = '';
send SOCK, "HELO localhost\r\n", 0;
recv SOCK, $junk, 512, 0;
if ($junk =~ /^5/) {$err = "true";}
send SOCK, "mail from: <IISArchive\@webloyalty.com>\r\n", 0;
recv SOCK, $poo, 512, 0;
if ($poo =~ /^5/) {$err = "true";}
send SOCK, "RCPT To:<$to>\r\n", 0;
recv SOCK, $poo, 512, 0;
if ($poo =~ /^5/) {$err = "true";}
send SOCK, "DATA\r\n", 0;
recv SOCK, $poo, 512, 0;
if ($poo =~ /^5/) {$err = "true";}
send SOCK, "From: IISArchive\@webloyalty.com\r\n", 0;
send SOCK, "Date: ".localtime()."\r\n", 0;
send SOCK, "To: $to\r\n", 0;
send SOCK, "Subject: $subject\r\n", 0;
send SOCK, "\r\n$message\r\n.\r\n", 0;
recv SOCK, $poo, 512, 0;
if ($poo =~ /^5/) {$err = "Yes";}
send SOCK, "QUIT\r\n", 0;
recv SOCK, $poo, 512, 0;
if ($poo =~ /^5/) {$err = "Yes";}
close SOCK;
}
Why won't this program work when scheduled? All I get is a blank email, so I know the script is trying to run. And yes, I know it's rather ugly and I should really use the Net:SMTP object, but I'm not trying to be too pretty right now... build the walls and roof first, put the furniture in after it's watertight.
Thanks in advance
-- Matt