G'day Bod,

Adding "." to @INC has security implications. It used to be a default but was removed in Perl v5.26.0. There's a long discussion in "perl5260delta: Removal of the current directory (".") from @INC" which I recommend you read.

As already pointed out, cron provides a very limited environment. This not only includes the environment variables available, but also the content of those variables: compare the value of $ENV{PATH} from the command line and from a cron script.

To avoid problems, I typically use absolute paths everywhere in a cron script. You may find code like this in my cron scripts:

... PERL=/usr/bin/perl ... $PERL /full/path/to/some_script.pl ... $PERL /other/full/path/to/other_script.pl ...

A lib directory is fairly standard for Perl modules; it is not standard for scripts. A bin directory is more usual for *.sh, *.pl, and so on. I'll leave you to decide if you wish to make a change.

I knocked up a skeleton directory structure to mirror what you presented. My /home/ken/tmp/pm_11153844_cron_paths/ is intended as an equivalent to your /home/username/website/prod/.

ken@titan ~/tmp/pm_11153844_cron_paths $ pwd /home/ken/tmp/pm_11153844_cron_paths ken@titan ~/tmp/pm_11153844_cron_paths $ ls -lR * lib: total 1 -rw-r--r-- 1 ken None 633 Aug 13 00:38 maintain.pl drwxr-xr-x 1 ken None 0 Aug 13 00:36 Site lib/Site: total 1 -rw-r--r-- 1 ken None 78 Aug 13 00:36 Utils.pm template: total 0 www: total 0

An extremely minimal Site::Utils module looks like this:

ken@titan ~ $ cat /home/ken/tmp/pm_11153844_cron_paths/lib/Site/Utils.pm package Site::Utils; use strict; use warnings; our $VERSION = '1.2.3'; 1;

Here's a maintain.pl which shows how to determine various directories and tests them:

ken@titan ~ $ cat /home/ken/tmp/pm_11153844_cron_paths/lib/maintain.pl # standard pragmata use strict; use warnings; # code to get absolute paths use Cwd 'abs_path'; use File::Basename 'dirname'; my ($BIN_DIR, $LIB_DIR, $TEMPLATE_DIR, $WWW_DIR); BEGIN { $BIN_DIR = dirname abs_path __FILE__; $LIB_DIR = abs_path "$BIN_DIR/../lib"; $TEMPLATE_DIR = abs_path "$BIN_DIR/../template"; $WWW_DIR = abs_path "$BIN_DIR/../www"; } # test paths print "\$BIN_DIR[$BIN_DIR]\n"; print "\$LIB_DIR[$LIB_DIR]\n"; print "\$TEMPLATE_DIR[$TEMPLATE_DIR]\n"; print "\$WWW_DIR[$WWW_DIR]\n"; # test 'Site::Utils' found use lib $LIB_DIR; use Site::Utils; print "Site::Utils version: $Site::Utils::VERSION\n";

Here's that script run from my home directory:

ken@titan ~ $ pwd /home/ken ken@titan ~ $ /usr/bin/perl /home/ken/tmp/pm_11153844_cron_paths/lib/maintain.pl $BIN_DIR[/home/ken/tmp/pm_11153844_cron_paths/lib] $LIB_DIR[/home/ken/tmp/pm_11153844_cron_paths/lib] $TEMPLATE_DIR[/home/ken/tmp/pm_11153844_cron_paths/template] $WWW_DIR[/home/ken/tmp/pm_11153844_cron_paths/www] Site::Utils version: 1.2.3

If you do decide to use a bin directory, that script should still work as is. The first line of output would be:

$BIN_DIR[/home/ken/tmp/pm_11153844_cron_paths/bin]

The other output should be unchanged.

— Ken


In reply to Re: use lib "." by kcott
in thread use lib "." by Bod

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.