"service"-command and environment variables

Solution 1:

As of Fedora 16, service only accepts LANG and TERM environment variables, everything else gets discarded. So, even if your current {CentOS,RHEL} accepts the variables somehow, be prepared for the future where it does not work any more.

So, hard coding the init script and/or setting up the variables in the daemon settings file itself would be your choices.

Solution 2:

It is recommended to place configuration settings in a configuration in /etc/sysconfig/<servicename> which is then read by the init script.

Regards

Bram

Solution 3:

From man 5 init:

   Job environment
       Each  job  is run with the environment from the events or commands that started it.  In addition, you may define defaults in the
       job which may be overridden later and specify which environment variables are exported into the events generated for the job.

       The special UPSTART_EVENTS environment variable contains the list of events that started the job, it will not be present if  the
       job was started manually.

       In  addition,  the  pre-stop  and post-stop scripts are run with the environment of the events or commands that stopped the job.
       The UPSTART_STOP_EVENTS environment variable contains the list of events that stopped the job, it will not be present if the job
       was stopped manually.

       All  jobs  also contain the UPSTART_JOB and UPSTART_INSTANCE environment variables, containing the name of the job and instance.
       These are mostly used by the initctl(8) utility to default to acting on the job the commands are called from.

       env KEY[=VALUE]
              Defines a default environment variable, the value of which may be overriden by the event or command that starts the  job.
              If  ´KEY=VALUE´ is specified, the variable KEY is given the value VALUE.  If only ´KEY´ is given, then the value is taken
              from the init(8) daemon's own environment.

       export KEY
              Exports the value of an environment variable into the starting(7), started(7), stopping(7) and stopped(7) events for this
              job and to all resultant events (not just those relating to the current job).

Additionaly you can do grep env /etc/init/* to see how is used

This is my output:

/etc/init/container-detect.conf:env container
/etc/init/container-detect.conf:env LIBVIRT_LXC_UUID
/etc/init/container-detect.conf:    # is to check for "container" in init's environment.
/etc/init/container-detect.conf:        [ -d /proc/vz ] && [ ! -d /proc/bc ] && container=openvz
/etc/init/mounted-debugfs.conf:env MOUNTPOINT=/sys/kernel/debug
/etc/init/mounted-dev.conf:env MOUNTPOINT=/dev
/etc/init/mounted-proc.conf:env MOUNTPOINT=/proc
/etc/init/mounted-tmp.conf:env MOUNTPOINT=/tmp
/etc/init/munin-node.conf:env DAEMON=/usr/sbin/munin-node
/etc/init/mysql.conf:env HOME=/etc/mysql
/etc/init/nginx.conf:env DAEMON=/usr/local/nginx/sbin/nginx
/etc/init/nginx.conf:env PID=/usr/local/nginx/logs/nginx.pid
/etc/init/procps.conf:env UPSTART_EVENTS=
/etc/init/rc.conf:env INIT_VERBOSE
/etc/init/rc-sysinit.conf:env DEFAULT_RUNLEVEL=2
/etc/init/rc-sysinit.conf:env RUNLEVEL=
/etc/init/rc-sysinit.conf:env PREVLEVEL=
/etc/init/rc-sysinit.conf:env INIT_VERBOSE
/etc/init/wait-for-state.conf:env TIMEOUT=30
/etc/init/wait-for-state.conf:env MANUAL_OVERRIDE="N"
/etc/init/wait-for-state.conf:env WAIT_FOREVER="N"
/etc/init/wait-for-state.conf:env WAIT_STATE="started"
/etc/init/wait-for-state.conf:env TARGET_GOAL="start"

And for a exhaustive example see some of that scripts. Here nginx.conf:

# nginx

description "nginx http daemon"
author "Philipp Klose "

start on (filesystem and net-device-up IFACE=lo)
stop on runlevel [!2345]

env DAEMON=/usr/local/nginx/sbin/nginx
env PID=/usr/local/nginx/logs/nginx.pid

expect fork
respawn
respawn limit 10 5
#oom never

pre-start script
 $DAEMON -t
 if [ $? -ne 0 ]
 then exit $?
 fi
end script

exec $DAEMON