[sf-perl] Running a perl script as a service

Joe Brenner doom at kzsu.stanford.edu
Fri Jan 11 13:03:10 PST 2008

Quinn Weaver <quinn at fairpath.com> wrote:
> Richard Reina wrote:
> > Any ideas as to what I'm doing wrong?  Thanks for any help.
> I think the problem is this:
> When you run it from the command line, it uses your personal
> environment var settings, including your setting for PERL5LIB.
> When it runs from your init.d script, it doesn't have your environment--
> just a minimal env setup provided by the init system.
> To avoid confusion, I would use an explicit path here:
> >       require "mail_processor.pl";
> e.g. require '/path/to/mail_processor.pl';

I might try adding a line like:

  use lib '/path/to';

Once you've found one problem like that, it's likely that others will
turn up.  I suggest writing a small script that does things like echo
your environment variables to a log file, then you can run it from
init.d and also from the command-line.  Compare the output of the
two runs, looking for differences that are likely to cause problems
(PERL5LIB, PATH, etc).

Here's a copy of my "echo_env_to_log" if you're interested:

# echo_env_to_log                   doom at kzsu.stanford.edu
#                                   19 Sep 2007

use warnings;
use strict;

use File::Basename qw( basename );

### use Env qw(HOME);

our $VERSION = 0.01;

my $prog = basename($0);

my $label = shift;

my $home = "/home/doom";  # Can't use $HOME: becomes "/root" when you're root.
my $logfile = "$home/tmp/$prog.log";
open my $lf, ">>", $logfile or die "$!";

print {$lf} "\n";

if ($label) {
  print {$lf} "$label:\n";

print {$lf} "Running: $0\n";

my $datestamp = `date`;
print {$lf} "at $datestamp\n";

foreach my $k (keys %ENV) {
   print {$lf} "$k=\"$ENV{$k}\"\n";

More information about the SanFrancisco-pm mailing list