Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Environment Variables?????

by SriniK (Beadle)
on Jun 27, 2012 at 09:23 UTC ( #978597=perlquestion: print w/replies, xml ) Need Help??
SriniK has asked for the wisdom of the Perl Monks concerning the following question:


I have a script which working perfect if we run manually, but if we put as cron job its not working.

I guess it might be the environment issue. i have defined my envs in .bashrc

Can somebody help me on this

Srini K

Replies are listed 'Best First'.
Re: Environment Variables?????
by Corion (Pope) on Jun 27, 2012 at 09:27 UTC
Re: Environment Variables?????
by Anonymous Monk on Jun 27, 2012 at 09:39 UTC
Re: Environment Variables?????
by mrguy123 (Hermit) on Jun 27, 2012 at 11:02 UTC
    Hi there
    First of all if you give more info (code, errors with the scripts etc.) it will always help you get a better answer
    Besides that, when I run Perl scripts via crontab, I always source the .cshrc (in your case I guess it is the .bashrc). You can see an example below, and maybe it will solve your problem
    25 18 * * * /bin/tcsh -c 'source ~/.cshrc; perl
    Good Luck,
    Mister Guy


      In general I discourage that practice. Instead I will either (a) set the environment variables in the crontab, or (b) create a specific dotfile (such as .cron_foo) to set up the variables.

      The reason? It really sucks when you edit your .cshrc/.bashrc and your cron jobs start failing...


      When your only tool is a hammer, all problems look like your thumb.

        I am also face this same issue in past. At the time i set the environment variable in crontab file, it's working fine as expected.

        Hi, roboticus
        Thanks for the advice, its always good to hear about how to program a bit smarter
        In my case I am working on a server where there is a very large number of env variables, so I was wondering if updating two files each time I add an env var is the right way to go?
        What do you think would be the best solution in this case?
Re: Environment Variables?????
by aaron_baugher (Curate) on Jun 27, 2012 at 18:52 UTC

    A simple way to see what environment variables and values cron gets is to create a script like the one below and put it in crontab with a time coming soon. For instance, if it's 8:10 now:

    $ cat /home/abaugher/doit #!/bin/sh set | sort $ chmod 700 /home/abaugher/doit $ crontab -l 12 8 * * * /home/abaugher/doit >/tmp/doit

    At 8:12, this will run that script and save the output to /tmp/doit, which you can then view to see what it knows.

    Aaron B.
    Available for small or large Perl jobs; see my home node.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2017-07-21 07:21 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (319 votes). Check out past polls.