in reply to Re^5: Continuous or timed?
in thread Continuous or timed?

Could the location of the modules be an issue here as I've added use lib '.';?

Yes, definitely, don't rely on the CWD either when running from cron, use an absolute path. Also, BTW, you can see what Perl is outputting by appending e.g. 2>/tmp/stderr to the crontab entry.

Replies are listed 'Best First'.
Re^7: Continuous or timed?
by stevieb (Canon) on Dec 14, 2020 at 16:59 UTC

    Agreed on all counts.

    Here's an example crontab entry I've got on some of my Pi CI systems where I start a test listener program at reboot. It logs all the output to a dedicated file:

    @reboot /path/to/perl /path/to/bbtester start > /tmp/cron_bbtester.log + 2>&1

    Regarding the use lib problem, there is a solution to allow the finding of libs regardless of where one runs the script from. Here's a simple module, that is located in the same directory the script is in (~/scratch/demo for this example):

    package Module; use warnings; use strict; use Exporter; our @ISA = qw(Exporter); our @EXPORT_OK = qw(flail_around); sub flail_around { print "Flailing like a dying chicken!\n"; }

    Here's the script. Note the use of $RealBin. That variable contains the directory where the script resides. It's equivalent to ., if one is actually within that directory:

    use warnings; use strict; use FindBin qw($RealBin); use lib $RealBin; use Module qw(flail_around); flail_around();

    Output when I run the script from within that dir:

    spek@scelia ~/scratch/demo $ perl script.pl Flailing like a dying chicken!

    Output if I run the script from somewhere else within the file system:

    spek@scelia ~ $ perl scratch/demo/script.pl Flailing like a dying chicken!

    So it's very similar to use lib '.', but it uses the absolute path which is generated on the fly by looking up which directory the script is actually in. If your modules were in a lib/ dir within the directory, change use lib $RealBin; to use lib "$RealBin/lib";.

      Thank you stevieb - that is really helpful and everything I needed to get the RPi controller working.

      There is still an updater to write but, now that the main part works, that should be relatively straightforward.

Re^7: Continuous or timed?
by Bod (Parson) on Dec 14, 2020 at 17:13 UTC

    In the shared hosting environment I am used to, '.' is included in @INC. Without recompiling Perl - which I am not going to attempt! - or including use lib '.';, what is the best way to use modules I have created in a way that works from both CLI and cron?

    Are you suggestion I use an absolute path in the use statement?

    Update...

    I've just realised you meant to use an absolute path in use lib '/path/to/modules/';

    I'm obviously having a senior moment today!

      See the second half of my updated post here for the best way to manage the use lib '.'; problem.