System/getenv can't read env vars?

2,252 views
Skip to first unread message

Jarrod Swart

unread,
Jul 7, 2014, 2:45:10 PM7/7/14
to clo...@googlegroups.com
If I call:

(System/getenv "MY_VAR") ;=> nil

From bash 

set | grep MY_VAR ;=> foo

echo $MY_VAR' ;=> foo

I can do this from bash, as well as my tmux session.  I use tmuxinator to launch a repl and vim, thus my repl process is running inside TMUX.  That said, I can see the env var in other tmux sessions.

Anyone ever run across this issue?  Am I doing something wrong?  The point of this was to 12 Factor App-ify my environment for setting various runtime config options.  Is there another/better way to do the same thing that others prefer.

Thanks,

Jarrod

François Rey

unread,
Jul 7, 2014, 2:47:24 PM7/7/14
to clo...@googlegroups.com

Softaddicts

unread,
Jul 7, 2014, 2:48:55 PM7/7/14
to clo...@googlegroups.com
I assume you did

export MY_VAR

before starting you REPL ?

Luc P.
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@googlegroups.com
> Note that posts from new members are moderated - please be patient with your first post.
> To unsubscribe from this group, send email to
> clojure+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google Groups "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>
--
Softaddicts<lprefo...@softaddicts.ca> sent by ibisMail from my ipad!

Softaddicts

unread,
Jul 7, 2014, 2:49:59 PM7/7/14
to clo...@googlegroups.com
And did you test this by setting it from .bashrc ?

Jarrod Swart

unread,
Jul 7, 2014, 2:57:48 PM7/7/14
to clo...@googlegroups.com
Yep I ran

export MY_VAR=foo

and all subsequent "calls" to get or print the env var on the CLI work fine.

I set the var and restarted all repl processes.

I'll try setting it in .bashrc as well.

Sean Corfield

unread,
Jul 7, 2014, 3:28:12 PM7/7/14
to clo...@googlegroups.com
(! 540)-> export MY_VAR=foo

Mon Jul 07 12:26:01
(sean)-(jobs:0)-(~/clojure)
(! 541)-> lein repl
nREPL server started on port 56747 on host 127.0.0.1 - nrepl://127.0.0.1:56747
REPL-y 0.3.1
Clojure 1.6.0
    Docs: (doc function-name-here)
          (find-doc "part-of-name-here")
  Source: (source function-name-here)
 Javadoc: (javadoc java-object-or-class-here)
    Exit: Control+D or (exit) or (quit)
 Results: Stored in vars *1, *2, *3, an exception in *e

user=> (System/getenv "MY_VAR")
"foo"

How are you starting your REPL processes? Maybe you're starting them from an environment that does not pick up the same environment?

Sean
signature.asc

Jarrod Swart

unread,
Jul 7, 2014, 3:32:18 PM7/7/14
to clo...@googlegroups.com
Sean,

Thanks for the input.  There is a good chance you are right.  The process is launched from tmuxinator a tool that opens multiple processes and joins them all to a tmux session.

I suppose I'll have to investigate that but it seems odd that if I type "echo $MY_VAR" I get the expected value from within that same series of tmux processes.

It must be part of the JVM bit I don't grasp yet.

I'll look into these suggestions later.

Is there a particular way you do per environment/machine config that differs from env vars?

James Reeves

unread,
Jul 8, 2014, 4:47:43 AM7/8/14
to clo...@googlegroups.com
You could try running:

    export MY_VAR=foo
    bash -c 'echo $MY_VAR'

And see if that comes with the same result. You might also want to make sure you're not using a pre-loader like Drip.

You can use Java system properties to do per environment/machine config as well.

I wrote a library a while back that handles both system properties and environment variables, called environ: https://github.com/weavejester/environ

- James


--

Jarrod Swart

unread,
Jul 8, 2014, 11:07:18 AM7/8/14
to clo...@googlegroups.com, ja...@booleanknot.com
James,

It appears Sean and yourself have identified the issue.  The tmux scripts that launch my dev environment don't pull in the necessary environment.  

I have seen your environ library and I was to thank you for all your contributions.  Sean too!

While I have you James do you have a good example of an upstart script for uberjars handy?  

I think I saw you post one somewhere but I can't find it despite my search attempts.

James Reeves

unread,
Jul 8, 2014, 11:24:49 AM7/8/14
to Jarrod Swart, clo...@googlegroups.com
I use an upstart script like:

description "Some description"
author "Your name"

start on startup
stop on shutdown

setuid deploy
chdir /deploy
console log

env PORT=4000

exec java -jar uberjar-name.jar

In the above case, I deploy to the "/deploy" directory, and execute the jar using a non-privileged user named "deploy".

- James

Jarrod Swart

unread,
Jul 8, 2014, 11:38:29 AM7/8/14
to clo...@googlegroups.com, jcs...@gmail.com, ja...@booleanknot.com
Awesome thank you!
Reply all
Reply to author
Forward
0 new messages