大网数据广告活动
服务器运维问答

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

· 52CE 我爱测

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 …

问题原文

原帖链接 · Fnux · CC BY-SA 4.0

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.

已采纳解答

原帖链接 · Fnux · CC BY-SA 3.0

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

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2013-10-25)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0 / CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。