Search code examples
upstart

How to concatenate two string environment /w env stanza in Upstart?


Here is a part of my .conf file.

env SERVICE_ROOT="/data/service_root"
env LOG_DIR="$SERVICE_ROOT/logs"

and I checked all variables with following..

echo "\n`env`" >> /tmp/listener.log 2>&1

I expect that $LOG_DIR is "/data/service_root/logs" but what I got is..

SERVICE_ROOT=/data/service_root
LOG_DIR=$SERVICE_ROOT/logs

Did I miss something?


Solution

  • Defined environment variable is not accessible to the Job Configuration File itself.

    Upstart allows you to set environment variables which will be accessible to the jobs whose job configuration files they are defined in.

    As explained in 8.2 Environment Variables:

    Note that a Job Configuration File does not have access to a user's environment variables, not even the superuser. This is not possible since all job processes created are children of init which does not have a user's environment.

    Defined variable $SERVICE_ROOT is accessible to defined job.

    # /etc/init/test.conf
    env SERVICE_ROOT="/data/service_root"
    
    script
      export LOG_DIR="$SERVICE_ROOT/logs"
      # prints "LOG_DIR='/data/service_root/logs'" to system log
      logger -t $0 "LOG_DIR='$LOG_DIR'"
      exec /home/vagrant/test.sh >> /tmp/test.log
    end script
    

    Variable $LOG_DIR exported in script block is available for processes called within the same block.

    #!/bin/bash -e
    # /home/vagrant/test.sh
    echo "running test.sh"
    echo "\n`env`" | grep 'LOG_DIR\|SERVICE_ROOT'
    

    After running sudo start test content of /tmp/test.log will be:

    running test.sh
    SERVICE_ROOT=/data/service_root
    LOG_DIR=/data/service_root/logs
    

    In syslog you will find:

    Jul 16 01:39:39 vagrant-ubuntu-raring-64 /proc/self/fd/9: LOG_DIR='/data/service_root/logs'