#Read environment variables

21 messages · Page 1 of 1 (latest)

brave citrus
#

I extracted out just the important pieces for this conversion. I have a simple Go app that I'm trying to convert to using environment variables, rather than having credentials in the code base. I cannot seem to get this to work as an Ubuntu service, and I'm not sure why.

Code:

import (
    "bufio"
    "fmt"
    "log"
    "os"
    "strconv"
    "time"

    mqtt "github.com/eclipse/paho.mqtt.golang"
    "github.com/joho/godotenv"
)

func getEnvVar(key string) string {
    err := godotenv.Load(".env")

    if err != nil {
        log.Fatal(err)
    }
    return os.Getenv(key)
}

func main() {
    var broker = getEnvVar("MQTT_HOST")
        fmt.Println(broker)
}

The fmt.Println writes the expected value when run interactively go run main.go or when running the built app go build -> ./shed_temp_monitor. The issue occurs when I try to build a service around this. The files are owned by the same user/group that runs the app. What am I missing?

[Unit]
Description=Raspberry PI CPU Temperature Monitor
After=network.target

[Service]
ExecStart=/home/mainely/shed_temp_monitor/shed_temp_monitor
Restart=always
User=mainely
StandardOutput=journal
StandardError=journal

[Install]
WantedBy=multi-user.target
wary cairn
#

you're missing specification of current directory?

brave citrus
#

Wouldn't the env load be of the current directory? Or does that directory change as part of running as a service. I'm thinking more in my Windows mind where that current directory would be the user's directory

wary cairn
#

Or does that directory change as part of running as a service.
yes, as far as I remember workdir for services is / by default

brave citrus
#

Yeah, that does resolve it. I was hoping to keep things relative rather than absolute

wary cairn
#

well, you shouldn't do what you're doing anyway

brave citrus
#

What would the recommendation be? I don't have a local password manager set up, yet

wary cairn
#

if you want to accept settings in env vars, just read env vars, not file

brave citrus
#

I tried that, but it wasn't working

wary cairn
#

and if you want to keep env vars in file, then use EnvironmentFile directive for systemd unit

#

systemd will load that file and so on

#

I tried that, but it wasn't working
then make it work

#

or maybe even just specify config file with all stuff

#

even better

brave citrus
#

Now thinking of it, I may not have used an absolute path when I used EnvironmentFile. I would rather keep things together than using no file, I'd just have to adjust how I manage the env vars because my other method (non-file) wasn't working. But I should be able to get that solved

wary cairn
#

speaking of keeping things relative, you can set workdir for systemd unit

#

WorkingDirectory=

brave citrus
#

Ah right, forgot about that. One thing about using EnvironmentFile, I was using absolute path before. It just wasn't loading the file (or at least, Go didn't recognize the env vars)

wary cairn
brave citrus
#

The file was set with 644

stoic nest
#

I don't understand why people use an extension when there is $[os.GetEnv]