http://www.perlmonks.org?node_id=978597

SriniK has asked for the wisdom of the Perl Monks concerning the following question:

Hi

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


Thanks
Srini K

Replies are listed 'Best First'.
Re: Environment Variables?????
by Corion (Patriarch) 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 run_something.pl
    Good Luck,
    Mister Guy

      mrguy123:

      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...

      ...roboticus

      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.