Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Perl & cron job environmental variables

by Anonymous Monk
on Jul 02, 2007 at 20:14 UTC ( #624540=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have to use Perl in a "cron" job.
However, my environmental variables seem not to export to Perl (but they do seem to export to csh.) Running the following "cron" job:

/usr/bin/csh -c printenv

Produces the correct set of environmental variables.

However, the Perl script "cron" job:

/home/username/bin/perlprintenv.pl

where the code is simply:

#!/usr/local/perl -w foreach $c (keys %ENV) { print "$c \t".$ENV{$c}."\n"; }
produces a much shorter list of variables and a shorter path. I am running Solaris.
What is the recommended "fix" for this problem?

Is there any easy way to force cron to export all variables?

Comment on Perl & cron job environmental variables
Download Code
Re: Perl & cron job environmental variables
by Anonymous Monk on Jul 02, 2007 at 20:20 UTC
    Update: I am using Perl 5.6.1
Re: Perl & cron job environmental variables
by Fletch (Chancellor) on Jul 02, 2007 at 20:26 UTC

    Jobs run from cron do not run under your login environment so yes, you'll seem to be missing a bunch of environment variables. Either explicitly set them in the crontab (which may depend on your particular OS' cron implementation), write a wrapper script which (say) sources your "normal" shell configuration file (e.g. ~/.cshrc or ~/.bashrc or what not) and then that is what runs your Perl, or set them explicitly with $ENV{FOO}="/bar/baz/quux"; statements in your code.

    And to fend off the likely next posting: don't try just doing system( "source ~/.cshrc" ) in your code; it won't work.

Re: Perl & cron job environmental variables
by dorko (Parson) on Jul 02, 2007 at 20:29 UTC
    What happens when you use csh to run your Perl program?

    I have no idea if this will work:

    /usr/bin/csh /usr/local/perl /home/username/bin/perlprintenv.pl
    (Other answers will be better, and this might not even work... It just feels quick and dirty.)

    Cheers,

    Brent

    -- Yeah, I'm a Delt.
Re: Perl & cron job environmental variables
by toolic (Chancellor) on Jul 02, 2007 at 20:49 UTC
    As Fletch mentioned, you can create a csh wrapper script around your Perl script:
    #!/bin/csh /home/username/bin/perlprintenv.pl
    then use a crontab entry like this:
    * * * * * /home/username/bin/wrapper.csh
    This worked for me on my solaris machine.
Re: Perl & cron job environmental variables
by Anonymous Monk on Jul 03, 2007 at 01:29 UTC
    Hi,

    We created a file with all the environment we needed,
    then called the cron job :

    + + + + + /usr/bin/csh source env.file; perljob.pl

    which worked just fine.

    We even put Sybase env variables in the file which meant
    we didn't need to put them in a BEGIN block.

    J.C.

Re: Perl & cron job environmental variables
by djp (Hermit) on Jul 03, 2007 at 03:33 UTC
    You might like an explanation of what's happening here. By default, csh reads your .cshrc file, unless the -f switch is specified. That's why your environment is populated for the csh script but not for the Perl script. It's good practice in csh scripts to specify '#!/bin/csh -f' on the shebang line, to prevent reading .cshrc. It's better practice to avoid csh scripts altogether, see http://www.faqs.org/faqs/unix-faq/shell/csh-whynot for details.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://624540]
Approved by philcrow
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2014-09-02 02:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (18 votes), past polls