0

I am using Ubuntu 14.04 and 16.10; so my init system can be either upstart or systemd.

Things like How to set an environment variable in Amazon EC2 over on Stack Overflow show how one fetches an instance ID using curl, then fetches instance tags using aws ec2 describe-tags, then imports them as environment variables into an interactive shell, or a shell script. One of the answers also shows how one runs those data though jq.

This is all well and good, but I don't need variables set in a shell script, or in an interactive shell. Having a shell script that is sourced as in those answers is no good to me, as there isn't a shell involved in where I need these things set.

I need them set in the environments of dæmon processes that are started from systemd or upstart. In the systemd case, in particular, service unit files are not shell scripts at all. To set environment variables one puts things like Environment= in the unit file.

So how can I create the last link in the chain? With what's on Stack Overflow I can get as far as reading the data and making environment-like var=val strings out of them. (I am actually reading instance metadata and user data but the principle is the same — I pull stuff with curl and then do the jq and other post-processing to make var=val out of it.)

How do I forge the final link in the chain and get these strings into the environments of specific services run by upstart and systemd?

  • I see that this is already sinking. Here; allow me. – JdeBP Jan 2 '17 at 8:54
  • Unless your init system is replacing an already running init system you will not have networking available to allow you to fetch something from a url – icarus Jan 2 '17 at 8:55
0

If you can get the strings in a file in the form of environment variable assignments, e.g.

SOME_NAME=some_value

then you could perhaps use the EnvironmentFile= setting in the unit file. More details in man systemd.exec.

And you could always replace the service you're starting with a script that first sources a file with the environment variable settings, then execs the original service. This should work with upstart too.

For example:

#!/bin/sh
. /some/where/environment-variable-settings-file
exec original-service "$@"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.