Skip to content
Advertisement

Unix /Linux – several applications using the same environment variables but different values?

I am looking for some advice on the following below:

I have several applications and tools on a server that requires the environment to be set up with variables etc for them to run.

Many of these applications use the same environment variables to run but with different values.

Some of these applications have multiple versions. e.g. prog v1.1, prog v1.2, prog v1.3 etc..

Example: prog v1.1 uses environment variable VAR1 = VAL1, but prog v1.2 needs VAR1 = VAL2 in order to run. It is the same variable, but different values required for each application.

Another example is: prog3 could require a number of environment variables set that prog4 doesn’t need.

There is also logic involved when setting up the environment for these applications, like if file exists; do this; else do that


I’ve created shell scripts e.g. prog1setup.sh, prog2setup.sh etc to setup the environment for each of these applications and then:

  1. start a new shell for every application that needs to run,
  2. run the shell script and then
  3. run that application so it inherits the environment variables

What I’d like to know is, are there any open source tools available out there that can be used to better manage this as there could be lots of applications? I’ve been doing some research and came across tools such as launcherd, supervisor and Environment modules but I have not used any of these before.

If anyone has used any of those, please provide some insight on if it can apply here or is there something else that could be suggested?


Thanks

Advertisement

Answer

Since you mentioned creating scripts, I would suggest creating prog1.sh with

#!/bin/bash
export VAR1=VAL1
prog1 "$@"

and creating prog2.sh with

#!/bin/bash
export VAR1=VAL2
prog2 "$@"

Then, never run prog1 or prog2 directly. Instead, run prog1.sh or prog2.sh. Since a shell scripts environment never affects the calling shell, the variable VAR1 will only exist when needed and will disappear once the script finishes executing.

Alternative

Some people prefer functions to scripts because function definitions can be conveniently stored in ~/.bashrc. As examples of functions that set needed environment variables:

date2() ( export TZ=Asia/Tokyo; date "$@" )
date3() ( export TZ=Europe/Paris; date "$@" )

These can be used as follows:

$ date2
Wed Jul  8 14:49:30 JST 2015
$ date3
Wed Jul  8 07:49:31 CEST 2015
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement