Yes and no.
You can read an environment, but as cron jobs run their own environment, you need to load the environment you want. See Get default login environment.
| [reply] |
Thanks for the heads up. I'm a bit confused though. In he example you link to do I need to change the rest of my script or does $ENV get overwritten by the function? Do I just call %ENV = (%ENV, get_login_env()); and continue as I did before With my $varname = $ENV{'ENV_VAR_NAME'};? Thanks
| [reply] |
The function returns a hash. What you do with that hash is up to you. You haven't shown your program, so I can only give you general advice.
One approach would be to set up %ENV with the newly found values. Another one would be to merge %ENV with the newly found values, as you (and the example) show. A third approach would be to keep the two sets of values separate.
To simulate running under the login environment as closely as possible, %ENV = get_login_env() will certainly do that, but in the process, you lose any indication that your program is running as cron job. In general, I think the approach of merging %ENV and the values from the login environment, with the values from the login environment taking precedence is a good one.
| [reply] [d/l] [select] |
You can access environment variables in a script run as a cron job but take note that cron jobs often have only a sub-set of the environment available to a user shell. For instance, you will likely have a minimal $PATH under cron. It might be worth writing a small script to dump the environment to a file then run it as a cron job to see what's available.
I hope this is helpful.
| [reply] [d/l] |