Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am trying to start a service that requires a env. variable to be set to certain path. I set this variable in "/etc/profile.d/". However when I start this service using the service command, it doesn't work.

man service:

service runs a System V init script in as predictable environment as possible,
removing most environment variables and with current working directory set to /.

So it seems that service is removing my variables. How should I set the variables up to keep them from being removed. Or is that something i should not do.

I could start the service manually using the init-scripts, or even hardcode the path into the script, but I'd like to know how to use it with the service command.

share|improve this question

3 Answers 3

up vote 4 down vote accepted

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.

share|improve this answer
    
The downside is that every time I update the app I have to change the file, but I guess I just have to... (...not update often) –  Esa Varemo Mar 28 '12 at 14:45
    
Well, the "update often" is a norm nowadays. Live with it. –  Janne Pikkarainen Mar 28 '12 at 14:46

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
share|improve this answer

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

Regards

Bram

share|improve this answer
    
As another tip in the /etc/sysconfig/<servicename> file you can use bash's source to load files from /etc/profile.d to imitate the shell login environment. –  Adam Gent Mar 18 '13 at 5:21

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.