What environment variables are available while starting the script using init.d boot process?

StackOverflow https://stackoverflow.com/questions/2598815

  •  25-09-2019
  •  | 
  •  

Question

My problem is like this (OS is Sun Solaris):

  1. At the boot time I want to start a process using a specific script. I am doing this by putting this script in /etc/init.d (and following other K and S rules).

  2. The program which will be called by the script is located at $HOME/xxx/yyy location.

  3. I am using 'su - {myuser} -c "{full path of the program}"' in order to execute the script as {myuser}.

  4. I don't want to hardcode the value of $HOME in the script, but I want to use the $HOME environment variable only. How can I get this $HOME environment variable in the shell script? Also what other variables will be available to me?

Was it helpful?

Solution

To get the home variable of any user, try this hack:

HOME=`sudo -u myuser -s "cd ~; pwd"`

OTHER TIPS

Just put

env > env.txt

in the script. After running your script the env.txt file contains all environment variables.

2) Which user's $HOME? root's $HOME or myuser's $HOME

If it's myuser's home, just use $HOME in the child script, and don't use it in the init.d script. This will be set for you by the shell process you are starting. When you do su - {myuser} you are instructing su to run myuser's login shell (as defined in /etc/passwd). The login shell will set the default environment variables based on what shell it is. See your shell's documentation for details. On Solaris this is probably ksh or bourne shell.

3) Do su - myuser -c /usr/bin/env. That will print out the environment variables that will be set in the child script.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top