9

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.

Esa Varemo
  • 581
  • 3
  • 8
  • 21

4 Answers4

6

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

Regards

Bram

Bram
  • 1,121
  • 6
  • 9
  • As another tip in the `/etc/sysconfig/` 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 05:21
4

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.

Janne Pikkarainen
  • 31,852
  • 4
  • 58
  • 81
2

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
jperelli
  • 223
  • 5
  • 10
0

The SystemV/init.d script for your service is located here: /etc/init.d/<service-name>; and it loads environment variables from /etc/default/<service-name>

For example, concerning my service docker I have the following lines in /etc/init.d/docker:

BASE=docker

if [ -f /etc/default/$BASE ]; then
        . /etc/default/$BASE
fi

This shows very clearly how the /etc/default/<service-name> file is loaded, so if you wanna set environment variables for your service, just set them in this file

As an example, my file /etc/default/docker looks like this:

# Docker Upstart and SysVinit configuration file

#
# THIS FILE DOES NOT APPLY TO SYSTEMD
#
#   Please see the documentation for "systemd drop-ins":
#   https://docs.docker.com/engine/admin/systemd/
#

# Customize location of Docker binary (especially for development testing).
#DOCKERD="/usr/local/bin/dockerd"

# Use DOCKER_OPTS to modify the daemon startup options.
#DOCKER_OPTS="--dns 8.8.8.8 --dns 8.8.4.4"

# If you need Docker to use an HTTP proxy, it can also be specified here.
#export http_proxy="http://127.0.0.1:3128/"

# This is also a handy place to tweak where Docker's temporary files go.
#export DOCKER_TMPDIR="/mnt/bigdrive/docker-tmp"
Elouan Keryell-Even
  • 493
  • 2
  • 8
  • 21