Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^5: Continuous or timed?

by Bod (Parson)
on Dec 14, 2020 at 16:47 UTC ( [id://11125170]=note: print w/replies, xml ) Need Help??


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

What are the characteristics that you're using to identify that it runs at the CLI but not from cron?

It writes a timestamped line to a logfile very close to the beginning of the script.
The logfile uses an absolute path.

#!/usr/bin/perl use lib '.'; use Curtains::Control; use Bod::Config; use strict; my $DEBUG = 1; my $control = Curtains::Control->new; $control->log("Starting Curtain Controller") if $DEBUG;

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

Replies are listed 'Best First'.
Re^6: Continuous or timed?
by haukex (Archbishop) on Dec 14, 2020 at 16:50 UTC
    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.

      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.

      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11125170]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-25 09:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found