Hello fellow Monks,

I just finished running a script as a service. But it does not do anything when I run it as a service (but does the job if called directly with perl). Does anyone know why?
I used the following for adding it:
use strict; use Win32::OLE; use Config; use File::Basename; my $szPerlScript = shift @ARGV; my $szServiceName = shift @ARGV; my $szSrvAnyPath = shift @ARGV; unless (-e $szPerlScript) { die "Please give me a script\n"; } unless (-e $szSrvAnyPath) { die "srvany.exe not found\n"; } unless ($szServiceName) { $szServiceName = basename($szPerlScript); } my $szComputer = '.'; my $szPerlPath = $Config{perlpath}; $szPerlScript =~ s/\//\\/g; $szSrvAnyPath =~ s/\//\\/g; $szPerlPath =~ s/\//\\/g; use constant HKLM => 0x80000002; # Service Type use constant KERNEL_DRIVER => 1; use constant FS_DRIVER => 2; use constant ADAPTER => 4; use constant RECOGNIZER_DRIVER => 8; use constant OWN_PROCESS => 16; use constant SHARE_PROCESS => 32; use constant INTERACTIVE_PROCESS => 256; my $INTERACT_WITH_DESKTOP = 0; my $_NOTIFED; # Error Control use constant NOT_NOTIFIED => 0; use constant USER_NOTIFIED => 1; use constant SYSTEM_RESTARTED => 2; use constant SYSTEM_STARTS => 3; # Create Service my $hWMI = Win32::OLE->GetObject('winmgmts:\\\\'.$szComputer.'\\root\\ +cimv2'); my $hService = $hWMI->Get('Win32_Service'); my $wResult = $hService->Create($szServiceName, $szServiceName, $szSrv +AnyPath, OWN_PROCESS, !$_NOTIFED, 'Automatic', $INTERACT_WITH_DESKTOP +, 'NT AUTHORITY\\LocalService', ''); if ($wResult > 0) { print "Error creating service $szServiceName: $wResult\n"; exit 0; } else { print "Successfully created service $szServiceName: $wResult\n"; } # Create Registry Entry my $szKeyPath = 'SYSTEM\\CurrentControlSet\\Services\\'.$szServiceName +.'\\Parameters'; my $hReg = Win32::OLE->GetObject('winmgmts:\\\\'.$szComputer.'\\root\\ +default:StdRegProv'); $hReg->CreateKey(HKLM, $szKeyPath); $hReg->SetStringValue(HKLM, $szKeyPath, 'Application', $szPerlPath); $hReg->SetStringValue(HKLM, $szKeyPath, 'AppParameters', $szPerlScript +); print "Created registry values\n"; # Start Service $hService = $hWMI->Get('Win32_Service.Name=\''.$szServiceName.'\''); $wResult = $hService->StartService(); if ($wResult > 0) { print "Error starting service $szServiceName: $wResult\n"; } else { print "Successfully started service $szServiceName\n"; }

The scripts I tested are:
my $szPath = 'C:\winzip.log'; while (1) { unlink $szPath; select(undef, undef, undef, 60); }

and
use Wx qw(:everything); use Wx::Locale qw(:default); use Wx::Event qw(:everything); my $hApp = Handler->new(); $hApp->MainLoop(); package Handler; use strict; use Wx qw(:everything); use Wx::Event qw(EVT_TIMER); use base qw(Wx::App); sub new { my ($class) = @_; my $this = $class->SUPER::new(); return $this; } sub OnInit { my ($this) = @_; $this->{hWnd} = Wx::MessageDialog->new(undef, 'Timer Message'); $this->{wId} = Wx::NewId(); $this->{hTimer} = Wx::Timer->new($this, $this->{wId}); $this->{hTimer}->Start(1000); EVT_TIMER($this, $this->{wId}, \&OnTimer); return 1; } sub OnTimer { my ($this, $hEvent) = @_; # Stop Timer while processing $this->{hTimer}->Stop(); if (-e 'c:/winzip.log') { unlink 'c:/winzip.log'; } $this->{hWnd}->ShowModal(); # Restart Timer for more $this->{hTimer}->Start(1000); }

Any suggestions what I have to do to make the scripts do what I want them to do (as Service), as they work when executed directly? Does anyone know some good references on Windows Services and Perl Scripts (just ordered "Windows XP Cookbook", but will be delivered next week)?
Used: Perl 5.8.8 on Windows XP SP2.

Andre

In reply to Perl script does not do anything when run as Windows Service by Outaspace

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.