Outaspace has asked for the wisdom of the Perl Monks concerning the following question:

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

Replies are listed 'Best First'.
Re: Perl script does not do anything when run as Windows Service
by Crackers2 (Parson) on Dec 07, 2006 at 19:11 UTC

    The most common cause that I can think of is that services by default run as a special user (LOCALSYSTEM on win2k I believe; I don't run XP so can't check there) which does not necessarily have the same permissions as the normal user you tried to run this as.

    You can change the user the service runs as from the services control panel, or you can make sure the default user has sufficient privileges.

Re: Perl script does not do anything when run as Windows Service
by quester (Vicar) on Dec 07, 2006 at 23:43 UTC
    To amplify on Crackers2's comment, one of the things that the "Local System account" in Windows does not have is network access; it has no credentials that are valid across systems.

    You can double click on your service in Control Panel -> Admin Tools -> Services, select the Log On tab, and enter an appropriate user account and password. This can be a domain account if the local and remote machines are in the same domain, or, by typing in the machinename\userid, an account on the remote machine.

Re: Perl script does not do anything when run as Windows Service
by bingos (Vicar) on Dec 08, 2006 at 07:18 UTC