Linux – How to load the environment variables at boot time before X11 on Ubuntu Precise

bootenvironment-variableslinuxUbuntu

Ubuntu Precise 64 bit, I'm facing a problem that I'm unable to solve and that I'll try to describe below:

I'm using a console mode program (let's say abc) that uses Go, NodeJS, Java and Scala.

In order for abc to work with these languages, I've to declare the following statements:

a) within /etc/environment:

PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin"
CLASSPATH=$CLASSPATH:/usr/share/java/scala-library.jar

b) within /etc/login.defs

ENV_SUPATH  PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin
ENV_PATH  PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin

c) a) within /etc/sudoers:

# env_reset

Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin"

Then, when I start abc from a terminal, all is fine and I can use any of the 4 languages described above.

However, if I put a script within /etc/init.d that starts abc during the boot process (i.e. before to start the GUI), using Java from abc still is fine, but using Go, NodeJS or Scala doesn't work anymore.

Then, I guess that during the boot process, the script within /etc/init.d that starts abc is executed before that the different environment variables set within /etc/sudoers, /etc/environment and /etc/login.defs are loaded.

So, my question is: how to force the environment variables to be loaded before that my script starting abc is launched?

Any help and advice on this topic would be truly appreciated.


Thanks again to Mark and Danila.

Below is the Danila's modified "abc" script file that I put within /etc/init.d


#!/bin/sh

##### EDIT: ADD THIS VARS DEFINITIONS:
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin" 
CLASSPATH=$CLASSPATH:/usr/share/java/scala-library.jar
"ENV_SUPATH PATH"="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin"
"ENV_PATH PATH"="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin"
"Defaults secure_path"="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin"

##### EXPORT this VARS so they are accessible to children:"
export "PATH" "CLASSPATH" "ENV_SUPATH PATH" "ENV_PATH PATH" "Defaults secure_path"

### BEGIN INIT INFO
# Provides:          abc
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: abc initscript
# Description:       This initscript starts and stops abc
### END INIT INFO

# Author: Fnux, fnux.fl at gmail dot com
# Version: 1.2
# Note: (edit ABC_PATH if abc isn't installed in /opt/abc)

NAME=abc
ABC_PATH=/opt/abc
START="-d"
STOP="-k"
VERSION="-v"
SCRIPTNAME=/etc/init.d/$NAME
STARTMESG="\nStarting abc in deamon mode."
UPMESG="\n$NAME is running."
DOWNMESG="\n$NAME is not running."
STATUS=`pidof $NAME`
# Exit if abc is not installed
[ -x "$ABC_PATH/$NAME" ] || exit 0
case "$1" in
  start)
    echo $STARTMESG
    cd $ABC_PATH
    ./$NAME $START
    ;;
  stop)
    cd $ABC_PATH
    ./$NAME $STOP
    ;;
  status)
    if [ "$STATUS" > 0 ] ; then
      echo $UPMESG
    else
      echo $DOWNMESG
    fi
    ;;
  restart)
    cd $ABC_PATH
    ./$NAME $STOP
    echo $STARTMESG
    ./$NAME $START
    ;;
  version)
    cd $ABC_PATH
    ./$NAME $VERSION
    ;;
  *)
    echo "Usage: $SCRIPTNAME {start|status|restart|stop|version}" >&2
    exit 3
    ;;
esac
:

Now, when executing this script (ie. sudo service abc start) I get the following error messages:

fnux@fx8150-1204-3:~$ sudo service abc start
/etc/init.d/abc: 4: /etc/init.d/abc: ENV_SUPATH PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin: not found
/etc/init.d/abc: 5: /etc/init.d/abc: ENV_PATH PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin: not found
/etc/init.d/abc: 6: /etc/init.d/abc: Defaults secure_path=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin: not found
/etc/init.d/abc: 7: export: ENV_SUPATH PATH: bad variable name 
fnux@fx8150-1204-3:~$ 

So, where am I wrong?

TIA for an explanation how to fix this script.

Best Answer

OK, I think I found the problem: Never use double quotes (") for a VAR.

So, below is the right script:

#!/bin/sh
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin" 
CLASSPATH=$CLASSPATH:/usr/share/java/scala-library.jar
ENV_SUPATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin"
ENV_PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin"
export PATH CLASSPATH ENV_SUPATH ENV_PATH

### BEGIN INIT INFO
# Provides:          abc
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: abc init script
# Description:       This init script starts and stops abc
### END INIT INFO

# Author: Fnux, fnux.fl at gmail dot com
# Version: 1.2
# Note: edit ABC_PATH if abc isn't installed in /opt/abc

NAME=abc
ABC_PATH=/opt/abc
START="-d"
STOP="-k"
VERSION="-v"
SCRIPTNAME=/etc/init.d/$NAME
STARTMESG="\nStarting abc in deamon mode."
UPMESG="\n$NAME is running."
DOWNMESG="\n$NAME is not running."
STATUS=`pidof $NAME`
# Exit if abc is not installed
[ -x "$ABC_PATH/$NAME" ] || exit 0
case "$1" in
  start)
    echo $STARTMESG
    cd $ABC_PATH
    ./$NAME $START
    ;;
  stop)
    cd $ABC_PATH
    ./$NAME $STOP
    ;;
  status)
    if [ "$STATUS" > 0 ] ; then
      echo $UPMESG
    else
      echo $DOWNMESG
    fi
    ;;
  restart)
    cd $ABC_PATH
    ./$NAME $STOP
    echo $STARTMESG
    ./$NAME $START
    ;;
  version)
    cd $ABC_PATH
    ./$NAME $VERSION
    ;;
  *)
    echo "Usage: $SCRIPTNAME {start|status|restart|stop|version}" >&2
    exit 3
    ;;
esac
:

Thanks for your help.

Hope this may help others.

Cheers. ;)