How to load the environment variables at boot time before X11 on Ubuntu Precise?
Posted
by
Fnux
on Server Fault
See other posts from Server Fault
or by Fnux
Published on 2013-10-25T23:35:59Z
Indexed on
2013/10/26
3:56 UTC
Read the original article
Hit count: 585
Using 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 trully appreciated.
TIA. Cheers.
Thanks again to Mark and Danila.
Below is the current "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 iniscript 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
:
So, where and how should I write the needed environment variables for:
a) Go needs the following statements
(ie: PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin"
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
`# env_reset
Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin")
b) and Scala needs this one: (ie CLASSPATH=$CLASSPATH:/usr/share/java/scala-library.jar).
TIA for an explanation how to do so.
Cheers.
© Server Fault or respective owner