Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

Cheers. ;)

share|improve this question
2  
Rather than messing about with all that simply set (and export) the environment variables in the /etc/init.d script itself. –  Mark Wagner Oct 25 '13 at 23:38
    
Thank you Mark, but I don't know how to do so. Could you please explain how to set and export the environment variables in the /etc/init.d script? I guess you're talking about my abc script that start the abc executable. If yes, how should I declare these environment variables. TIA and thanks again for the hint. –  Fnux Oct 26 '13 at 0:09
    
Well if init.d abc script kicks off all others apps where you need those env variables available if you export all those vars in your abc script they will get passed to all other kicked off apps(children processes) –  Danila Ladner Oct 26 '13 at 0:25
    
So just do "export PATH=$PATH:whatever_path_you_need_to_append". Also the same with other variables you need to have to pass to children processes. –  Danila Ladner Oct 26 '13 at 0:26
    
Thank you Danila, but I'm quite a newbee and I'm not sure how to set the Go and Scala environment variables within my /etc/init.d/abc script. I put this script in the next answer (to be readable). –  Fnux Oct 26 '13 at 0:38
show 9 more comments

1 Answer 1

up vote 1 down vote accepted

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. ;)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.