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

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 $@;

Replies are listed 'Best First'.
Re^8: Read UNIX environment variable
by Corion (Patriarch) on Feb 09, 2011 at 14:32 UTC

    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

        The cron daemon usually sends the output of a program via mail to the owner submitting the job. Find out how to configure your cron daemon to do that, and/or consult with your system administrator how to modify your system setup to do this.

        Alternatively, write debugging information from your program to a file so you can see how your program progresses and what values it finds, and where.