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


in reply to Re: Environment Variables?????
in thread Environment Variables?????

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.

Replies are listed 'Best First'.
Re^3: Environment Variables?????
by k_manimuthu (Monk) on Jun 27, 2012 at 11:47 UTC

    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.

Re^3: Environment Variables?????
by mrguy123 (Hermit) on Jun 27, 2012 at 12:35 UTC
    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?

      Just set up an environment for the cron job that has all the information (and permissions and so on) it needs to run properly. I bet a lot of your personal environment information consists of stuff such as a customised PATH, a prompt (likely PS1, PS2 etc), maybe something to tell Perl to look into a directory in your HOME to look for modules (e.g. PERL_LIB = ~/perl5lib), maybe some stuff to auto-beautify the output of the ls command, yadayadayada. Does the cron job really need all that? Or could it live a happy life with just a minimal environment?

        I agree with you that my private environment information (which is alarmingly similar to what you described) is not very relevant for the cron job. In my case, however, I am working on a product where there are close to a 100 "public" env variables which are used widely in every program (they are stored in a different .cshrc file which is sourced by my private one).
        Therefore, the idea of updating a separate file each time I need to use a different env variable seems like a recipe for mistakes.
        So, do you think there is there an "in-between" solution or do we agree to disagree?

      mrguy123:

      No, you don't want to update two files. Instead, you can source one from the other. For example, you can put all your aliases and personal stuff in your .bashrc. The application configuration stuff, you could put in .app_config. Then, in your .bashrc, you can source .app_config.

      However, if your application needs a large number of env variables, it might be better to make a configuration file for your application rather than relying on environment variables.

      Another possibility is to just use a separate account for your cron jobs. Then you can set it up any way you like, and when you modify your personal account, it won't mess up anything in the account you use for cron jobs.

      ...roboticus

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

        Thanks for the detailed answer
        I will keep this in my mind for my future cron jobs
      My suggestion would be to put your environment variables in a config file and have your script read the config file and add the vars to the %ENV hash as part of its initialization.