1

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?

blsn
  • 1,077
  • 3
  • 18
  • 38
  • Which variable specifically is not available where exactly? – Etan Reisner Nov 10 '15 at 19:07
  • ABC=123 in ~/.profile – blsn Nov 10 '15 at 19:08
  • `ABC=123` or `export ABC=123`? It needs to be exported for any child processes to use it. – chepner Nov 10 '15 at 19:09
  • What do you expect `~` is evaluating to when your script is started by the init/startup framework/service? – Etan Reisner Nov 10 '15 at 19:23
  • @ chepner, yep, of course: ```export ABC=123```, I've tried ```source ~/.profile``` and ```. ~/.profile``` both without success. – blsn Nov 10 '15 at 19:23
  • @ Etan Reisner, If I understand your question, so ```~/.profile``` is located in ```{HOME}/.profile``` , this is where I put ```export ABC=123```. I expect the startup app (Jetty) to get this key/value (secret key) at server startup. – blsn Nov 10 '15 at 23:16
  • ~/.profile means the roots home dir, correct? – cristi Nov 11 '15 at 12:43
  • The tilde (~) symbol stands for your home directory. If you are user, then the tilde (~) stands for ```/home/profile``` – blsn Nov 12 '15 at 15:10

1 Answers1

0

[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.

blsn
  • 1,077
  • 3
  • 18
  • 38