Linux

如何在 Ubuntu Precise 上的 X11 之前的啟動時載入環境變數?

  • June 2, 2020

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 之前)將腳本放入/etc/init.d啟動 abc 中,從 abc 使用 Java 仍然可以,但使用 Go、NodeJS 或 Scala 不再適用。

然後,我猜在啟動過程中/etc/init.d,啟動 abc 的腳本在其中設置不同的環境變數之前執行/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 了解如何修復此腳本。

好的,我想我發現了問題:永遠不要對 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
:

謝謝你的幫助。

希望這可以幫助其他人。

乾杯。;)

引用自:https://serverfault.com/questions/548798