Solved

How do I call batch file from Perl script when script is running as service?

Posted on 2011-02-28
9
853 Views
Last Modified: 2012-08-13
I have a Perl script that calls a batch file (running on Windows Server 2003).  I can run the Perl script from a command line, perl -w, and I don't get any errors.  But when I run the same script as a service, using srvany, then I get "The handle is invalid." several times per execution in my log file.  The batch file does execute correctly.  I'm new to Perl, so I don't know what's going on.  What is causing the handle is invalid errors when executing as a script?  
Perl Script
============
use strict;
use IO::Handle;
use File::Spec;
use File::stat;
require Win32::ChangeNotify;
#       Set config values
my $log_file = 'E:\SAPInterface\Logs\SAPOutboundLOG.txt';
my $cycle_time = "15";
my $wrapper_exe = 'E:\SAPInterface\Logs\TestThis.bat';
#   Open the log file for appending
open STDERR, '>>', $log_file;
while(1)
{
   print STDERR "before call to batch file\n";
   my $ec = system($wrapper_exe, "hi", "there");
   print STDERR "after call to batch file\n";
#      If return code is zero we are good to go!
   if($ec == 0)
   {
      print STDERR "CMD: $wrapper_exe ...[SUCCESSFUL]\n";
   }
   else
   {
      print STDERR "CMD: $wrapper_exe ...[FAILED]\n";
   }
   sleep ($cycle_time);
}
============================
Batch file
============================
echo off
echo start >> e:\sapinterface\logs\test.log
echo %1 %2 >> e:\sapinterface\logs\test.log
echo end >> e:\sapinterface\logs\test.log

Open in new window

SAPOutboundLOG.txt
test.log
0
Comment
Question by:oneDayAtaTime
9 Comments
 
LVL 16

Expert Comment

by:sjklein42
ID: 34998841

Using "@" instead of "echo off", does this change the behavior?

Please try replacing the batch file with these three lines:

@echo start >> e:\sapinterface\logs\test.log
@echo %1 %2 >> e:\sapinterface\logs\test.log
@echo end >> e:\sapinterface\logs\test.log 

Open in new window

0
 

Author Comment

by:oneDayAtaTime
ID: 34999287
I replaced the batch file with your suggested code.  No change to the output of the batch file, still works.  However, the number of handle is invalid lines increased in the log file.  Here is a sample from the log file:

before call to batch file
The handle is invalid.
The handle is invalid.
The handle is invalid.
The handle is invalid.
The handle is invalid.
The handle is invalid.
The handle is invalid.
The handle is invalid.
The handle is invalid.
The handle is invalid.
The handle is invalid.
The handle is invalid.
The handle is invalid.
The handle is invalid.
The handle is invalid.
The handle is invalid.
The handle is invalid.
The handle is invalid.
The handle is invalid.
The handle is invalid.
after call to batch file
CMD: E:\SAPInterface\Logs\TestThis.bat ...[SUCCESSFUL]

The Perl script is really designed to run a different batch file.  I dummied up a simple one as a test and example for this post.  My Perl experience is non-existent, this is my first attempt.  Since it only has these error messages when ran as a service, does Perl do something strange with the system call?  My original thought was to trap the STDOUT and send that to a variable, but that did not work (or I implemented it incorrectly).
0
 
LVL 13

Expert Comment

by:Carl Bohman
ID: 35001455
Does the same problem exist if you try to execute a simple built-in command like "dir" or an executable or another Perl script (instead of a batch file)?

This may be an issue with the way srvany works.  Services have some specific requirements that srvany is supposed to handle, but my experience is that it has some issues, especially where scripts are involved or where file extensions are relied on to know how to execute the command.  (My specific experience was with .vbs scripts.)
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 16

Expert Comment

by:sjklein42
ID: 35001698
Maybe we can see if using the backtick operator instead of system call makes a difference.

This should be equivalent to what you were doing with system:

my $mycommand = "$wrapper_exe hi  there";
`$mycommand`;   # do command
$ec = $?;     # pick up status

Open in new window

0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35001771
Does your service have "Allow service to interact with desktop" turned on?  This switch is found on the "Log On" tab of the service Properties for your service.

Try turning it on if it isn't already.
0
 
LVL 28

Expert Comment

by:FishMonger
ID: 35001984
Is the code you posted the exact and complete code you ran which generated those error messages?

That appears to be a Windows error message, not a perl error message.

My first recommendation would be to recode it and handle all of the functionality of the batch file directly in the Perl script.

Second recommendation would be to use Win32::Daemon instead of srvany.
http://search.cpan.org/~jdb/Win32-Daemon-20110117/Daemon.pm
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35003745
Here's another thing to try.  See if adding "start" to the command changes things.

my $wrapper_exe = 'start E:\SAPInterface\Logs\TestThis.bat';

Open in new window

0
 

Accepted Solution

by:
oneDayAtaTime earned 0 total points
ID: 35008833
I found the solution.  The invalid handle messages are showing up because the batch file is trying to output to the screen, which is the default for STDOUT in Perl.  When running as a service, it cannot do this.  So I added the following lines to the Perl script to redirect STDOUT to the Windows "bit bucket".

#   Redirect the standard output to the Windows null device
close STDOUT;
open STDOUT, '>', File::Spec->devnull;
0
 

Author Closing Comment

by:oneDayAtaTime
ID: 35045581
My solution worked and was not suggested by anyone else.
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Setting up a Microsoft WSUS update system is free relatively speaking if you have hard disk space and processor capacity.   However, WSUS can be a blessing and a curse. For example, there is nothing worse than approving updates and they just have…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question