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

I'm trying to write a service and need the current running directory for locating configuration files. I'm trying to use Cwd::abs_path($0) to get the directory and of course stripping off the exe name. The service seems to start, but the actual PerlSvc::Startup is never called. Everything up to that point seems to work fine ( i can see all the prints and even create a log file). If i comment out the use Cwd line, and also hardcode the program directory, the service runs like a charm. anyone have any ideas?
package PerlSvc; use warnings; use strict; use File::Copy; use System::System_Constants; use Cwd qw(abs_path); use File::Spec; use Getopt::Long; our $progname = Constants::APPLICATION_SERVICE_NAME_SHORT; our $path = undef; $path = abs_path($0); print "Path: " . $path . "\n"; our ($volume, $directory, $fileName) = File::Spec->splitpath($path); my $program_dir= $volume . $directory; print "Program Dir: " . $program_dir . "\n"; our $cscc = $program_dir . "cscc.exe"; our $serviceOptionsFile = $program_dir . "serviceOptions.xml"; our $timeout = ""; our $frequency = ""; our $schedule = ""; our $scapScan = ""; our $ovalScan = ""; our $installScan = 0; our %Config = (ServiceName => "SCC_Service", DisplayName=> "SCC_Service", StartType => 'auto', # auto, manual Interactive => 1, # boolean Description => 'Service with added console', StartNow => 1, Password => undef, UserName => undef, Dependencies => '', Parameters => '' ); my $logfile = $program_dir. 'SCC_Service.log'; print "Logfile: $logfile\n"; Log("\n ProgName: $progname \n"); ###################################################################### +#################### sub Startup . . . .

Replies are listed 'Best First'.
Re: PerlSVC and Cwd::abs_path
by kcott (Archbishop) on May 26, 2012 at 12:53 UTC

    The code you have posted seems to work fine to the extent I can test it. I left out the sub Startup ... and made 3 changes:

    #use System::System_Constants; ... our $progname = q{Constants::APPLICATION_SERVICE_NAME_SHORT}; ... #Log("\n ProgName: $progname \n");

    I simply tested with:

    $ cat pm_perlsvc.pl #!/usr/bin/env perl use strict; use warnings; use PerlSvc;

    which produces

    $ pm_perlsvc.pl Path: /Users/ken/tmp/pm_perlsvc.pl Program Dir: /Users/ken/tmp/ Logfile: /Users/ken/tmp/SCC_Service.log

    I see you've used File::Spec->splitpath() to get the path elements. For portability, I would recommend using File::Spec->catpath() rather than the concatenation operator (.) when joining path elements.

    You haven't shown the code where you are attempting to call PerlSvc::Startup() which makes debugging from this end rather difficult.

    I notice a lot of variables declared with our. If they need to be declared like this, check that you're not modifying $PerlSvc::path, $PerlSvc::directory, etc. outside of the PerlSvc package; if our $whatever is not a requirement, change it to my $whatever.

    If none of that helps, post the code with the call to PerlSvc::Startup() for a better answer.

    -- Ken

      It seems the Cwd::absolutePath() function call does not work correctly anymore (Hence the "These functions are exported only on request" in the perl doc). My current throws an error 1/5 times where the variable, even when initially initialized, throws an uninitialized error after the $absolutePath = Cwd::absolutePath(); assignment.

      Solution was to use the Cwd::cwd() function call.

      PS: Scary how the absolutePath function was still used even when not exported by request.
Re: PerlSVC and Cwd::abs_path
by thargas (Deacon) on May 28, 2012 at 11:55 UTC

    It's unclear to me what you want. You say you want the current running directory, but then you call abs_path($0) which will give you a path to the script itself. If you want a path to the script, it's more usual to use cpan:FindBin. If you really want the current directory, I think you want to look at cpan:Cwd at cwd() or getcwd() instead of abs_path().