How to Determine the Precise Set of Environment Variables a Systemd Environmentfile Would Set

How can I determine the precise set of environment variables a systemd EnvironmentFile would set?

The surest thing is to let systemd parse the file itself, via a transient service. Thus:

# emit a NUL-delimited set of key=value definitions for environment variables defined by a set of files
newVarsForFile_nullsep() {
local -a extraParams=( ); local file
(( $# )) || return 0 # no files specified, nothing to do
for file in "$@"; do
extraParams+=( --property=EnvironmentFile="$file" )
done
comm -z -23 \
<(sort -z < <(systemd-run --user --pipe "${extraParams[@]}" grep -zvE '^INVOCATION_ID=' /proc/self/environ </dev/null)) \
<(sort -z < <(systemd-run --user --pipe grep -zvE '^INVOCATION_ID=' /proc/self/environ </dev/null) )
}

# emit code that can be eval'd in an instance of bash to precisely define the exact variables
newVarsForFile_shellscript() {
while IFS= read -r -d '' vardef; do
printf '%s=%q\n' "${vardef%%=*}" "${vardef#*=}"
done < <(newVarsForFile_nullsep "$@")
}

Thereafter, one may invoke (as an example):

newVarsForFile_shellscript /etc/conf.d/*.conf

...to emit a shell script fragment which, when executed by bash, will set all the same environment variables that adding the relevant EnvironmentFiles to a service definition would set.

How to access and set Environment Variables any production (Nginx and Gunicorn)

What are you using to supervise and run the gunicorn process in Ubuntu? If you're not using any, I recommend you to use systemd, there's a small guide on how to setup in the gunicorn docs: https://docs.gunicorn.org/en/stable/deploy.html#systemd

After that, you can set the environment variables in the systemd config file doing like the following, under the [Service] section of the systemd config file:

[Service]
Environment="VAR_EMAIL=var-email"
Environment="ANOTHER_VAR=another-var"

You can also use the EnvironmentFile directive if you prefer to have these variables in a separate file: https://www.freedesktop.org/software/systemd/man/systemd.exec.html#EnvironmentFile=

How to set environment variable or system property in spring tests?

You can initialize the System property in a static initializer:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:whereever/context.xml")
public class TestWarSpringContext {

static {
System.setProperty("myproperty", "foo");
}

}

The static initializer code will be executed before the spring application context is initialized.

systemd script - environment file updated by ExecStartPre

Since at least systemd 237, you can write an file in ExecStartPre= that's read by EnvironmentFile= before ExecStart= runs as long as you prefix the file path with a dash (EnvironmentFile=-/some/path/.env).

This appears to due to the timing of when the environment is evaluated. poettering says:

the env vars are determined only at execution time

So it's an error if EnvironmentFile= is not defined when ExecStartPre= is executed which is why it needs to be optional, but when the "execution time" of ExecStart= comes, the EnvironmentFile is apparently re-checked and the environment variables set.

This could be better documented in man systemd.exec.

You can also use an alternative approach which is suggested in man systemd.exec where Environmentfile= is documented:

Use one systemd service to write the environment file and a second one to consume it. Using either a Before= or After= relationship in the [Unit] section, you can ensure that the service that writes the environment file is started first in the boot sequence.

Laravel 5.2 not reading env file

If any of your .env variables contains white space, make sure you wrap them in double-quotes. For example:

SITE_NAME="My website"

Don't forget to clear your cache before testing:

php artisan config:cache
php artisan config:clear

How to set env variable in Jupyter notebook

To set an env variable in a jupyter notebook, just use a % magic commands, either %env or %set_env, e.g., %env MY_VAR=MY_VALUE or %env MY_VAR MY_VALUE. (Use %env by itself to print out current environmental variables.)

See: http://ipython.readthedocs.io/en/stable/interactive/magics.html



Related Topics



Leave a reply



Submit