Setting environment variable globally in CentOS

Hello,

This should be simple, but it turns out to be a pain:  How do I set an environment variable in CentOS (3.9) so that it is globally available for all users as well as for all cron jobs?

The variable in question is PERL5LIB

Here is what I have tried:

/etc/rc.local:
export PERL5LIB=/d1/lib/perl

This did not do anything.  rc.local is perhaps being called as in a child shell, so the setting gets lost when shell exits?


/etc/profile:
export PERL5LIB=/d1/lib/perl

This works for user logins, but not for cron.


/etc/crontab:
PERL5LIB=/d1/lib/perl

This does not take effect for cron jobs (for some reason).


In the root cronjob itself:
PERL5LIB=/d1/lib/perl
*/1     *   *   *   *  echo $PERL5LIB > /tmp/t.txt

This again does not have any effect (the test cron job creates an empty file).


I would like to know if there is ONE place to put this so that it takes effect everywhere (or possibly two places: one for user logins and one for cron jobs).


This works, but I do not want the redundancy of having export statements all over the place:
*/1     *   *   *   *  export PERL5LIB=/d1/lib/perl ; echo $PERL5LIB > /tmp/t2.txt


Adding this line to the start of every perl script also works, but again I am looking for a more global solution to this:
use lib qw(/d1/lib/perl);

Any solution to this?

Thanks,
Lars
Lars007Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
JordanH155Connect With a Mentor Commented:
You stated in the original post that putting it in the root cron job didn't work, but it seems to work for me.

[root@myze ~]# crontab -l
TEMPTEST=/etc/test
*/1 * * * * echo $TEMPTEST > /root/test.txt

[root@myze ~]# cat test.txt
/etc/test
0
 
medveddCommented:
There is no one place to set variables for logins and cron. For logins it's /etc/profile,
for cron it's /etc/sysconfig/crond

0
 
medveddCommented:
Oops, I'm wrong. Variables in /etc/sysconfig/crond will affect only crond daemon, not cron jobs.
You have to set variables for each cron job individually.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
Lars007Author Commented:
Well, that is what I was suspecting, but found hard to accept.  This seems to be a limitation specific to only some distros (like RH / CentOS), since there are several references on the web stating:

"An active line in a crontab will be either an environment setting or a cron command. An environment setting is of the form,
name = value "

(http://linux.die.net/man/5/crontab).

Too bad that this is not supported in CentOS (nor does crontab -e complain when you save such a crontab, which is also odd.  If it is invalid, it should say something).

Before I close this, maybe someone else have a solution that we both are unaware of...

Lars

0
 
JordanH155Commented:
Have you tried creating a file with the needed variables in /etc/profile.d/ ?  I think this should take effect on login and non-login shells.  I'm not completely certain, but it's the only thing I can think of you have not already tried.
0
 
Lars007Author Commented:
Jordan, I tried your suggestion - it did not work.
0
 
Lars007Author Commented:
Jordan,

What distro are you using?

Lars
0
 
Lars007Author Commented:
Jordan,

I just tried this again, just copying and pasting the exact same lines I tried last week into the crontab job (which then only created an empty file), and it is now working.  I know I was not smoking crack, so am not sure what is going on....  Anyhow, thanks for pointing this out - I am all set now!

Lars
0
All Courses

From novice to tech pro — start learning today.