Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

A relative cron questions

by bittondb (Novice)
on Dec 11, 2001 at 12:28 UTC ( #130886=perlquestion: print w/replies, xml ) Need Help??
bittondb has asked for the wisdom of the Perl Monks concerning the following question:

Ok, so I have had the unfortunate happenstance to learn that cron runs from ~/ and not the path of the script. So this is what I did...
if(defined $ENV{DBUILDER_DIR}){ use lib "$ENV{DBUILDER_DIR}/lib"; } else { use lib "../lib"; }
and the crontab is
* */6 * * * DBUILDER_DIR=/home/indy/source/dbuilder /home/indy/source/ +dbuilder/scripts/spamburglar.pl
Is this the proper way to go about this? Just so you are aware, I also have this in a subsequent module:
if(defined $ENV{DBUILDER_DIR}){ $cfg->file("$ENV{DBUILDER_DIR}/etc/dbuilder.conf"); } else { $cfg->file("../etc/dbuilder.conf"); }
So, am I on the right track? Oh, and I know TIMTOWTDI!

Replies are listed 'Best First'.
Re: A relative cron questions
by atcroft (Abbot) on Dec 11, 2001 at 13:53 UTC

    Someone else please let me know if this is right, but my understanding was that you w/could do something like the following in a crontab(5) for such a thing:

    DBUILDER_DIR=/home/indy/source/dbuilder * */6 * * * $DBUILDER_DIR/scripts/spamburglar.pl

    To my knowledge, you don't have to put an environment variable definition in a cron entry on the same line, but can put it above entries if it affects more than one, or is more readable that way. Then, in your code, I am thinking you w/could do something like:

    unless (defined($ENV{'DBUILDER_DIR'})) { $ENV{'DBUILDER_DIR'} = '..'; } use lib "$ENV{'DBUILDER_DIR'}/lib";

    I haven't used the use lib... pragma you are using before, but I hope this either proves helpful in itself, or results in a more knowledgable monk posting to correct my misunderstanding, enlightening us both. My best in your quest for this knowledge.

      You can have environment variables per job, but there ought to be a semi-colon in there
      * */6 * * * DBUILDER_DIR=/home/indy/source/dbuilder;$DBUILDER_DIR/spam +burglar.pl
      If it on a non-job line it becomes available to all jobs.

      --
      perl -p -e "s/(?:\w);([st])/'\$1/mg"

Re: A relative cron questions
by hakkr (Chaplain) on Dec 11, 2001 at 17:25 UTC
    you can use chdir '/dir'; to change the current working directory of your script. Also you could add the path to your libraies to @INC
Re: A relative cron questions
by Fastolfe (Vicar) on Dec 12, 2001 at 02:10 UTC
    Also see Read a static file from module directory, which is a similar situation. You may be able to use $0 in conjunction with File::Basename to identify the location of your script and back track your way to the right lib directory. But I think the other posters' solutions are a little more elegant.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://130886]
Approved by root
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (9)
As of 2018-05-22 20:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?