如何在 Ubuntu Precise 上的 X11 之前在启动时加载环境变量?

如何在 Ubuntu Precise 上的 X11 之前在启动时加载环境变量?

Ubuntu Precise 64 位,我面临一个无法解决的问题,我将尝试在下面描述:

我正在使用一个使用 Go、NodeJS、Java 和 Scala 的控制台模式程序(假设为 abc)。

为了让 abc 能够与这些语言一起使用,我必须声明以下语句:

a) 在以下范围内/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) 在/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) 在以下范围内/etc/sudoers

# env_reset

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

然后,当我从终端启动 abc 时,一切都正常,我可以使用上面描述的 4 种语言中的任何一种。

但是,如果我在启动过程中(即在启动 GUI 之前)放置一个启动 abc 的脚本/etc/init.d,那么从 abc 使用 Java 仍然可以,但使用 Go、NodeJS 或 Scala 则不再起作用。

然后,我猜测在启动过程中,启动 abc 的脚本/etc/init.d会在设置的不同环境变量之前执行/etc/sudoers/etc/environment/etc/login.defs加载。

所以,我的问题是:如何在启动 abc 的脚本之前强制加载环境变量?

对于有关此主题的任何帮助和建议都将不胜感激。


再次感谢马克和丹尼拉。

下面是我放入 Danila 修改的“abc”脚本文件/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
:

现在,当执行此脚本(即sudo service abc start)时,我收到以下错误消息:

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:~$ 

那么,我错在哪里了?

TIA 解释如何修复该脚本。

答案1

好的,我想我找到问题了:永远不要对 VAR 使用双引号(“)。

因此,以下是正确的脚本:

#!/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
:

感谢您的帮助。

希望这可以帮助其他人。

干杯。;)

相关内容