What environment variables are available while starting the script using init.d boot process?
Question
My problem is like this (OS is Sun Solaris):
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).The program which will be called by the script is located at
$HOME/xxx/yyy
location.I am using
'su - {myuser} -c "{full path of the program}"'
in order to execute the script as {myuser}.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?
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.