in reply to use lib "."

cron has a virtually empty environment. Environment variables can be set in crontab or kept in a separate file and imported (via env), or you could just not use an environment variable, and programmatically determine that path within your code.

Replies are listed 'Best First'.
Re^2: use lib "."
by cavac (Prior) on Aug 13, 2023 at 09:35 UTC

    Correct. It also doesn't run the .bashrc and stuff. For this reason, i usually have cron call a bash script that THEN starts the perl script in question. Excerpt from my user crontab:

    20 1 * * * /bin/bash /home/cavac/src/pagecamel_cavac/devscripts/geoip/updategeoip.sh >> /home/cavac/src/pagecamel_cavac/devscripts/geoip/crontab.log 2>&1 &

    Note that this specifies the FULL path to the script. It also rereoute all STDOUT and STDERR into a logfile. Here's the script:

    #!/usr/bin/env bash . ~/.bashrc_cavac cd /home/cavac/src/pagecamel_cavac/devscripts/geoip date perl updategeoip.pl

    What this does is source the bashrc script that sets all the proper environments, change directory to the one the perl script expects, runs date (so the logfile contains a line with the timestamp of the run) and then runs the perl code.

    Just for reference, here's the bashrc-script. I removed parts that are not relevant for this answer; it actually does a lot more, depending on which system and for which user it's running:

    # CavacPerl export PATH=/home/cavac/bin/CavacPerl-5.36.1/bin:$PATH export MANPATH=/home/cavac/bin/CavacPerl-5.36.1/man:$MANPATH # Scripts export PATH=/home/cavac/bin/scripts:$PATH toilet -f smblock --filter metal:border virgo.cavac.at

    (The "toilet" command runs on every computer/account of mine that allows ssh login, to show a banner of the system i logged in. Mostly to prevent me debugging on the wrong machine and wondering why it doesn't work.)

    As a side note, running bash scripts to run your perl code also allows you to make sure you are running the most recent version of the script. On some of my raspberries is just run the appropriate mercurial SCM commands before running the perl script. Another win for total laziness, now i don't even have to update those ancient things manually...

    PerlMonks XP is useless? Not anymore: XPD - Do more with your PerlMonks XP