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