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

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:


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?

Replies are listed 'Best First'.
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 toolic (Bishop) 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/
    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 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/
    (Other answers will be better, and this might not even work... It just feels quick and dirty.)



    -- Yeah, I'm a Delt.
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 for details.
Re: Perl & cron job environmental variables
by Anonymous Monk on Jul 03, 2007 at 01:29 UTC

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

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

    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.


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

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2018-06-20 04:42 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (116 votes). Check out past polls.