Search code examples
bashdaemon

ENV value is not visible on server restart with bash daemon script


I have an embedded jetty app that I want to start run automatically in the background by using a ‘start-stop-daemon’ script. When I start the script as follows everything goes well.., and the ENV (environment variable) is visible to the startup app:

vagrant@homestead:~$ sudo /etc/init.d/myscript start

I have this in my script:

#!/bin/bash
:
:
DAEMON_USER=vagrant
:
:
# Source to load the secret key
source ~/.profile
:
:
start-stop-daemon --start --background --pidfile $PIDFILE --make-pidfile --user $DAEMON_USER --chuid $DAEMON_USER --chdir $MARY_BASE --startas $DAEMON -- $DAEMON_OPTS
:

When the script is automatically uploaded (server restart), the app is automatically start but the key value (ENV) is not visible to the startup app.

How to get the ENV value visible to the app on server startup with a daemon bash script?


Solution

  • [SOLVED]

    In ~/.profile you can place environment variable assignments, since it gets executed automatically during the start-up.

    A suitable file for system-wide environment variable settings that affect the system as a whole (rather than just a particular user) is /etc/environment.

    Copy your key (export ABC=123) to the above file and source it in your bash script (. /etc/environment).

    I hope it can help someone.