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

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
oneDayAtaTimeAsked:
Who is Participating?
 
oneDayAtaTimeConnect With a Mentor Author Commented:
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
 
sjklein42Commented:

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
 
oneDayAtaTimeAuthor Commented:
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
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
Carl BohmanCommented:
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
 
sjklein42Commented:
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
 
sjklein42Commented:
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
 
FishMongerCommented:
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
 
sjklein42Commented:
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
 
oneDayAtaTimeAuthor Commented:
My solution worked and was not suggested by anyone else.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.