Solved

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

Posted on 2011-02-28
9
843 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Recently, I had the need to build a standalone system to run a point-of-sale system. I’m running this on a low-voltage Atom processor, so I wanted a light-weight operating system, but still needed Windows. I chose to use Microsoft Windows Server 200…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

760 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now