Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I need the wisdom and help of all perl monks out there. Here is my problem..

I created this script to restart a remote services, but before it does, the script will take all logs file in that specified directory and create a folder based on the time stamped and move the logs into them first before it restarting the remote service.

When I run the script on an NT/win2k server I get the following error message. What am I missing or not doing correctly? It seems like "mkdir" does not work remotely on an NT platform....I would also like an email sent everytime this script runs telling me the remote service have been started (I want to use mime::lite as the email module)....

**Error Message I get when running the script on Win2K *** D:\Perl\bin>perl -w service.pl 10.1.1.5 Alerter usage: posix /c <path> [<args>] error creating \\10.1.1.5\Weblogiclogs\02091812! at service.pl line 27 +. ********************************************************** use Win32::Service; my $filedir = "\\\\10.1.1.5\\Weblogiclogs"; my $IWlogs = "\\\\10.1.1.5\\Weblogiclogs\\*.log*"; ############################################################### # Get Local Time and Create folder based on local time stamped ############################################################### ($sec,$min,$hour,$mday,$mon,$year) = localtime(time); $mon = (1,2,3,4,5,6,7,8,9,10,11,12)[$mon]; if($mon < 10){$mon = "0" . $mon;} # add a leading zero to one-di +git months if($mday < 10){$mday = "0" . $mday;} # add a leading zero to one-di +git days if($hour < 10){$hour = "0" . $hour;} # add a leading zero to one-di +git hours if($min < 10){$min = "0" . $min;} # add a leading zero to one-di +git minutes if($sec < 10){$sec = "0" . $sec;} # add a leading zero to one-di +git seconds ################################################## # Create folder based on local time obtained above # - This is where the script is erroring out at.... ################################################## if(!-e $createDir) { @args = ( "mkdir", $createDir); system(@args) == 0 or die "error creating $createDir!"; } ########################################################## # move logs files to new created directory named after # the local time. ########################################################### @args = ( "move" , $IWlogs , $createDir ) ; system( @args ) == 0 or die "error moving $IWlogs!"; ############################### # Restart remote Service ############################### $i=0; @ARGV = ('-') unless @ARGV; while ($ARGV = shift) { $home[$i] = $ARGV; $i = $i + 1; } $restart = 1; if ($restart == 1) {&StartServ($home[0],$home[1])}; sub StartServ { $computername = shift; $servicename = shift; Win32::Service::GetStatus("\\\\".$computername, $servicename, \%st +atus); die "service is arealdy started, please check and try again...\n" +if ($status{CurrentState} == 4); Win32::Service::StartService("\\\\".$computername,$servicename) || + die "Can't start service\n"; print "Your desired service is now started...Have a nice day!\n"; }

Edit by dws to fix formatting

Replies are listed 'Best First'.
Re: Please help - problem with script on Win2K
by BrowserUk (Patriarch) on Sep 18, 2002 at 19:21 UTC

    I notice several things with your code.

    First, you don't show where you actually create the $creadedir var, but from the error message

    error creating \\10.1.1.5\Weblogiclogs\02091812! at service.pl line 27.

    it shows that you are missing off the minutes and seconds? Is this intentional? Could this be a part of your problem?

    Anyway, this is more easily done using POSIX::strftime as I've shown below.

    You really should get into the habit of including $! (and on Win32 boxes where the operation involves calls to the OS, $^E, see perlvar) in your error messages, it will usually tell you WHY things went wrong, as well as that they have.

    As talexb says above the perlfunc:mkdir function would be better than that system call, and if your log files are currently on the same drive as their new directory, then using the built-in function perlfunc:rename will move them to their new home much more efficiently than shelling to the system move command.

    use Win32::Service; my $filedir = "\\\\10.1.1.5\\Weblogiclogs"; my $IWlogs = "\\\\10.1.1.5\\Weblogiclogs\\*.log*"; # Get Local Time and Create folder based on local time stamped # This is a much easier way of creating your directory name my $createdir = strftime( '%y%d%e%H%M%S', localtime); # Create folder based on local time obtained above # - This is where the script is erroring out at.... if(!-e $createDir) { # NOTE: The $! & $^E will tell you WHY it failed. mkdir $createdir or die "Couln't create $createdir because $!($^E) +\n"; } # move logs files to new created directory named after # the local time. @args = ( "move" , $IWlogs , $createDir ) ; system( @args ) == 0 or die "error moving $IWlogs reason $!($^E)"; # +NOTE: $! & $^E

    Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!
      when I tried running the script I get the following error message below. Is there something I'm missing (i.e. module..)? thanks again! D:\Perl\bin>perl -w x.pl Undefined subroutine &main::strftime called at x.pl line 6.

        Yes, you need use POSIX;. It's part of the standard distribution, so there's not installation to be done.

        Sorry! I should have mentioned it or added the line to the code.


        Cor! Like yer ring! ... HALO dammit! ... 'Ave it yer way! Hal-lo, Mister la-de-da. ... Like yer ring!
Re: Please help - problem with script on Win2K
by talexb (Chancellor) on Sep 18, 2002 at 18:43 UTC
    I'd replace the system call with a call to the built-in function mkdir, then look at the value of $! when it fails.

    It could also be that you don't have write permission where you are creating the directory, or the directory above the one you're creating doesn't exist.

    --t. alex
    but my friends call me T.