LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 09-09-2011, 05:14 AM   #1
Jerry Mcguire
Member
 
Registered: Jul 2009
Location: Hong Kong SAR
Distribution: RedHat, Fedora
Posts: 201

Rep: Reputation: 31
Environment Variable for good


Hello,

I have installed my application to a path, let's say /usr/myapp and defined an environment variable in /etc/bashrc so that everyone knows where myapp is installed to.

Code:
export MYAPP_HOME=/usr/myapp
As long as my pals login interactively, everything looks good. Yet when it comes to scheduled jobs in the crontab, the /etc/bashrc doesn't get sourced and therefore no such variable defined.

At first my solution was to include the line at the top of the `crontab -e` as:
Code:
MYAPP_HOME=/usr/myapp
0 18 * * 1-5  sh $MYAPP_HOME/scripts/some-script
It worked, but it didn't seem right to do so because any further modification of the variable in /etc/bashrc would not be effective in the user's cron job list, which was difficult to maintain and created confusion.

I wonder if there is any way to make an environment variable globally effective in any login or any user's cron job. e.g. like $TOMCAT_HOME.

Any ideas? Thanks.

J.
 
Old 09-09-2011, 05:35 AM   #2
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,624

Rep: Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651
then do not add it to a SHELL start-up
that file is read when a user logs in or bash is ran

for a red hat based system set that at boot
set it in a script in "/etc/profile.d"

Last edited by John VV; 09-09-2011 at 05:36 AM.
 
Old 09-09-2011, 10:16 AM   #3
Jerry Mcguire
Member
 
Registered: Jul 2009
Location: Hong Kong SAR
Distribution: RedHat, Fedora
Posts: 201

Original Poster
Rep: Reputation: 31
Code:
$ cat /etc/profile.d/myapp.sh 
#!/bin/bash
export MYAPP_HOME=/usr/myapp

$ crontab -l
* * * * *   echo "printng \$MYAPP_HOME as \"${MYAPP_HOME}\" ." >~/out.log

$ cat ~/out.log
printng $MYAPP_HOME as "" .

$
Have I missed something?
 
Old 09-09-2011, 02:34 PM   #4
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,624

Rep: Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651
a bit yes - a very basic with no checks
Code:
#!/bin/bash

MYAPP_HOME=/usr/myapp

export MYAPP_HOME
 
Old 09-09-2011, 04:37 PM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Basically you want the shell to act as a login shell. This is achieved using option -l either in the sha-bang at the beginning of the script or by executing the script itself using bash -l. In both cases the file .bash_profile is sourced.

Anyway, your case is a bit different since you want to set an environment variable and use it inside the same command line (so that using bash -l $MYAPP_HOME/scripts/some-script will fail because the variable is substituted with a null string before the .bash_profile is sourced). A workaround can be to explicitly source .bash_profile in the same command line, e.g.
Code:
0 18 * * 1-5  . $HOME/.bash_profile && $MYAPP_HOME/scripts/some-script
 
Old 09-10-2011, 09:42 AM   #6
Jerry Mcguire
Member
 
Registered: Jul 2009
Location: Hong Kong SAR
Distribution: RedHat, Fedora
Posts: 201

Original Poster
Rep: Reputation: 31
I guess I'll have to combine the suggestions of both of yours. Thanks.

Any further suggestions welcomed.
 
Old 09-10-2011, 10:29 AM   #7
tredegar
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Fedora38
Posts: 6,147

Rep: Reputation: 435Reputation: 435Reputation: 435Reputation: 435Reputation: 435
Quote:
Any further suggestions welcomed.
OK.

Why don't you just put your app at a location that is on everyone's $PATH, like /usr/local/bin/ ?

Then they don't care where it is, and they can run it just by typing its name.
 
Old 09-10-2011, 11:34 PM   #8
Jerry Mcguire
Member
 
Registered: Jul 2009
Location: Hong Kong SAR
Distribution: RedHat, Fedora
Posts: 201

Original Poster
Rep: Reputation: 31
umm.. It's not a couple of tools/scripts that I am looking at. It's more like an installation of a tree structure, with a app home directory, it's own bin,dat,rpt and stuff. Anyway, it's not designed as everybody's tool.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Environment variable tarunchawla Linux - Newbie 1 03-27-2010 11:19 PM
keep environment variable manelameur Ubuntu 13 02-05-2010 11:01 PM
Environment Variable khdani Linux - Newbie 1 10-05-2008 10:14 AM
Environment variable vedanu Programming 1 05-17-2007 07:20 AM
Environment variable.. manomohan Programming 1 02-26-2007 11:27 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration