2

I'm using aws cfn to start ubuntu instances, and I need to pass variables provided in cfn launch script to the scripts on the instance. For example, I may pass "db-server=db-2.domain.com" and there is a number of scripts on the instance, that want to know this address.

At the moment I create a file in /etc/profile.d setting vars with export, but this only works if bash is started as a login shell, which requires extra actions on my part to have these variables everywhere.

So I though that maybe I could instead create files in /usr/local/bin, e.g. get-db-host.sh with echo "db-2.domain.com" as content, this way they would be available to each script with no changes required.

Is it a good approach?

3
  • Please always mention your OS (Unix? OSX? Linux? Which distro?) and also the shell you use (granted, export implies bash but please clarify).
    – terdon
    Commented Oct 3, 2013 at 14:24
  • Well, as I said I use ubuntu, you're right about bash, but I'm looking for a general approach, not sure how a different distro would affect that.
    – Fluffy
    Commented Oct 3, 2013 at 14:28
  • Sorry, you're right, you did say Ubuntu. As for distros, you never know, some have different set ups with slight differences in the files they load. For example, I was not 100% sure that /etc/environment is a Linux thing and not specific to Debian.
    – terdon
    Commented Oct 3, 2013 at 14:29

3 Answers 3

2

Is it a good approach?

I'm pretty sure all the scripts you are talking about are shell scripts. In this case, you could just put all your variables in a configuration file:

db-server=db-2.domain.com

Place that in a standard location (e.g., /usr/local/etc/myapp/) and source it in the other scripts:

. /usr/local/etc/myapp

You can also use just plain /etc, obviously.

1

The recommended method is to just add the variables to /etc/environment. This file is read on login (from the Ubuntu docs):

Environment variable settings that affect the system as a whole (rather than just a particular user) should not be placed in any of the many system-level scripts that get executed when the system or the desktop session are loaded, but into

/etc/environment - This file is specifically meant for system-wide environment variable settings. It is not a script file, but rather consists of assignment expressions, one per line. Specifically, this file stores the system-wide locale and path settings.

So, just edit /etc/environment/ and add

db-server=db-2.domain.com
0

I would say no to that idea. That directory /usr/local/bin is meant for executables that are local to the box and not part of any particular package (usually).

Rather than make it some mysterious setup that you've doing, put the variables where they would plainly obvious to someone else that might need to perform your task.

I'd put them in a file in your $HOME directory and simply source that file from your $HOME/.bashrc.

Example

if [ -f $HOME/.my_aws_env ]; then
    . $HOME/.my_aws_env
fi

Or you can be cool and hip and do it in one line:

[ -f $HOME/.my_aws_env ] && . $HOME/.my_aws_env

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .