Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^6: Read UNIX environment variable

by Corion (Patriarch)
on Feb 09, 2011 at 12:44 UTC ( [id://887184]=note: print w/replies, xml ) Need Help??


in reply to Re^5: Read UNIX environment variable
in thread Read UNIX environment variable

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.

Replies are listed 'Best First'.
Re^7: Read UNIX environment variable
by Anonymous Monk on Feb 09, 2011 at 13:55 UTC
    Well, it works from the prompt but not via cron.
    use strict; use warnings; use MIME::Lite; # from the link you gave sub get_login_env { local %ENV; my $shell = shift || (getpwuid($<))[8]; my $env = `echo env | perl -e 'exec {"$shell"} -sh'`; if (wantarray) { my @pieces = ( $env =~ m/^(.*?)=((?:[^\n\\]|\\.|\\\n)*)/gm ); s/\\(.)/$1/g foreach @pieces; return @pieces; } else { return $env; } } %ENV = (%ENV, get_login_env()); my $mailvar = $ENV{'TESTMAILUSRS'}; my @mailUsers; my $msg = new MIME::Lite(); $msg->build( From => 'us@here.com', Subject => 'Email from perl via cron', Type => 'text/html', Data => 'testing', Debug => 1 ); for (split /[\s\n]/, $mailvar){ if($_){ $msg->add(To => $_); } } eval { $msg->send }; die "MIME::Lite->send failed: $@\n" if $@;

      How does it fail for you? Does sh pick up the correct environment? Maybe you set some environment variables in another rc file, or in .profile or .kshrc. Start some debugging.

        I don't get the email sent, I don't see an error message in the mailbox for the user cron is runing as. If I use the data dumper module and add print Dumper(\%ENV); to the code I can see the env vars when running it interactivly. Can you please let me know how I can debug this issue? Thanks

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://887184]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2024-04-25 09:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found