in reply to use lib "."

What am I overlooking here?

You are overlooking what the current directory is when running the cron script. Set up a trivial cron job which just prints the current working directory and then you will know. It's usually $HOME unless you change it.

There are, as usual, so many ways to tackle this. My usual approach is to have cron run a shell script which does all the set-up (change to the right directory, populate the environment etc.) and then at the last have that script exec the perl script. I've found this to be robust and secure (in that it doesn't leak any info). You can have the same shell script (or 2 copies of the same script) for both test and prod and just pass it a different argument to choose which path to run. eg:

#!/bin/bash if [ "$1" == 'prod' ]; then cd $HOME/website/prod else cd $HOME/website/test fi export DOCUMENT_ROOT=./www exec ./lib/maintain.pl

And in your crontab call it like

30 5 * * * $HOME/website/prod/maintain.sh prod

I do find it slightly odd that you have scripts in your lib dir. I tend to keep all my cron jobs in a cron dir for clarity.


🦛

Replies are listed 'Best First'.
Re^2: use lib "."
by Bod (Parson) on Aug 12, 2023 at 19:11 UTC
    I do find it slightly odd that you have scripts in your lib dir. I tend to keep all my cron jobs in a cron dir for clarity.

    On sites where I have more than one cron script I keep them in a scripts directory.

    In this case, all the modules have their own directories and this is the only script outside of the www directory. So it is the only thing in the lib directory other than more directories.

    Would you still use a cron directory even if you can be pretty sure there will only ever be this one script?

    You can have the same shell script (or 2 copies of the same script) for both test and prod

    No need - only production has the cron maintenance script...

      Would you still use a cron directory even if you can be pretty sure there will only ever be this one script?

      I would, yes. The only things I expect to see in the lib tree should be modules or POD files.


      🦛

        I would, yes

        Thanks hippo - on the advice of yourself and kcott my structure is getting changed as things are updated...

        On a similar topic, traditionally any cron tasks I had called the web script for that functionality which detected if it was running under cron or CGI by checking the $ENV{'DEFAULT_GATEWAY'} variable. More recently I have created separate scripts for each function that needs to run on cron.

        However, unless there is a different schedule, I am now grouping all that into one script.

        For example, I have a script that runs every morning that sends out emails on some days to users who have unread notifications and also sends emails to admins on other days to tell them about blogs that need reviewing. Plus, it does some daily tidying. Would you have a single script to deal with all the daily activities or multiple scripts?