oneDayAtaTime
asked on
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?
test.log
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
SAPOutboundLOG.txttest.log
ASKER
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\TestT his.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).
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\TestT
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).
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.)
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.)
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:
This should be equivalent to what you were doing with system:
my $mycommand = "$wrapper_exe hi there";
`$mycommand`; # do command
$ec = $?; # pick up status
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.
Try turning it on if it isn't already.
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
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
Here's another thing to try. See if adding "start" to the command changes things.
my $wrapper_exe = 'start E:\SAPInterface\Logs\TestThis.bat';
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
My solution worked and was not suggested by anyone else.
Using "@" instead of "echo off", does this change the behavior?
Please try replacing the batch file with these three lines:
Open in new window